Contents tagged with ASP.NET
ASP.NET MVC View - More than just a page view. Part I
Generate RSS View
Introduction
Recently I was rebuilding my web site using ASP.NET MVC.
I really need to finish it in some stage, and then I decided to give a try to the ASP.NET MVC.
During my work I discovered the views are much more than just a page view and actually I found some fancy usages of ASP.NET MVC views.
In short series of articles I will share some of my fancy usages of ASP.NET MVC View.Background
When building an ASP.NET MVC web site I start using views for generating pages, same as I have used WebForms. Then at some point I realized ASP.NET MVC View is much more than a page view/markup.
Actually with ASP.NET MVC we as asp.net developers are not tailored any more to a particular markup where form with runat=server is required. Further more, We are even not tailored to HTML as view markup any more.Using the Code
Having an ASP.NET MVC web site I came to a task to generate some RSS feeds. First, as usual, I started implementing an HttpHandler for that. Then I realized, I can use a controller and views and my view markup could be the RSS XML markup.
Here the code of my RSS generating:
- Part of my RssController where I provide the feeds data.
public class RssController : Controller {
#region Methods ///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Indexes this instance.
/// </summary>
/// <returns></returns>
public ActionResult Index() {
RssFeed feed = new RssFeed();
feed.Title = "Velio's Products";
feed.Description = "The list of all my products.";
feed.Link = HttpContext.IsDebuggingEnabled ? "/" : "http://artembg.com/";
bool flag = false;
foreach (Product product in Product.GetAll()) {
if (!flag) {
feed.PubDate = feed.LastBuildDate = product.ReleaseDate;
flag = true;
}
feed.Items.Add(new RssFeedItem {
Title = product.Title,
Description = product.Description,
Link = product.HomeUrl,
Guid = product.HomeUrl,
PubDate = product.ReleaseDate
});
}
return View(feed);
}
- The Index.aspx view. Take a note there is no space between the page definition (<%@ Page ... %>) and the XML markup.
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Artem.WebSite.Models.RssFeed>" %><?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
<title><%= Model.Title %></title>
<link><%= Model.Link %></link>
<ttl>10</ttl>
<pubDate><%= Model.PubDateString %></pubDate>
<lastBuildDate><%= Model.LastBuildDateString %></lastBuildDate>
<description><![CDATA[
<%= Model.Description %>
]]></description>
<% foreach (var item in Model.Items) { %>
<item>
<title><%= item.Title %></title>
<link><%= item.Link %></link>
<guid><%= item.Link %></guid>
<pubDate><%= item.PubDateString %></pubDate>
<description><![CDATA[
<%= item.Description %>
]]></description>
</item>
<% } %>
</channel>
</rss>
- The entire RssFeed class:
/// <summary>
///
/// </summary>
public class RssFeed {
#region Static Fields /////////////////////////////////////////////////////////////////////
List<RssFeedItem> _items;
#endregion
#region Properties ///////////////////////////////////////////////////////////////////////
public string Title { get; set; }
public string Description { get; set; }
public string Link { get; set; }
public DateTime PubDate { get; set; }
public DateTime LastBuildDate { get; set; }
public string PubDateString {
get {
return PubDate.ToUniversalTime().ToString("R");
}
}
public string LastBuildDateString {
get {
return LastBuildDate.ToUniversalTime().ToString("R");
}
}
public List<RssFeedItem> Items {
get {
return _items ?? (_items = new List<RssFeedItem>());
}
}
#endregion
}
- The entire RssFeedItem class:
/// <summary>
///
/// </summary>
public class RssFeedItem {
#region Properties ///////////////////////////////////////////////////////////////////////
public string Title { get; set; }
public string Description { get; set; }
public string Link { get; set; }
public string Guid { get; set; }
public DateTime PubDate { get; set; }
public string PubDateString {
get {
return PubDate.ToUniversalTime().ToString("R");
}
}
#endregion
}
Points of Interest
And because "Video worth a Million Words", take a look at my short video on this article here.
Once realizing the freedom We, ASP.NET developers, got with ASP.NET MVC Views, there is no limit of the views usage.
Stay tuned to the next part of this my short series about the ASP.NET Views.Happy coding ...
- Part of my RssController where I provide the feeds data.
ASP.NET MVC View - More then just a page view. Part II
Generate GSitemap View
Introduction
During my work on an ASP.NET MVC website I realized ASP.NET MVC views are more than just a page views and can be used to produce any kind of output.
In the previous part of this series I gave an example how RSS output can be produces using a ASP.NET MVC controller and view.
This time I will show how simple is to generate an output in Google sitemap (GSitemap) format and provide a dynamic sitemaps for Google crawler.Code
Having an ASP.NET MVC web site I came to a task to generate some dynamic Google sitemap for helping the Google crawler to discover easily the dynamic content.
Here the code of my GSitemap generating:
- Part of my Controller class where I provide the data.
/// <summary>
/// Gets the products Google sitemap.
/// </summary>
/// <returns></returns>
public virtual ActionResult GSitemap() {
return View(Product.GetAll());
}
- The view markup. Take a note there is no space between the page definition (<%@ Page ... %>) and the XML markup.
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Artem.WebSite.Models.Product>>" %><?xml version="1.0" encoding="utf-8" ?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<% foreach (var product in Model) { %>
<url>
<loc>http://artembg.com<%= product.DetailsUrl %></loc>
</url>
<% } %>
</urlset>
Points of Interest
Once realizing the freedom We, ASP.NET developers, got with ASP.NET MVC Views, there is no limit of the views usage.
Happy coding ...
- Part of my Controller class where I provide the data.
GoogleMap Control "Look&Feel" video published
I just published a new video: Using Skin to manage GoogleMap Control "Look&Feel".
Check it out hereRegards
GoogleMap Control 4.0 released
This release comes with a lot of new features like: StreetView; Reverse Geocoding; better AddressNotFound support; MarkerStyle for general way to manage the “Look&Feel” of markers; markers InfoWindow extensions and improvements; extended event handling; improved disposing and persistence; etc.
New Features
(For a full list check out GoogleMap Control 4.0 beta as well)
- MarkerStyle inner property of GoogleMap control is now available to master in more general way the "Look&Feel" of all the markers.
- It will for example simplify the way to set custom icon to all the markers in one place.
- InfoContent now provides a default implementation of container control for marker's info window content
- The InfoContent behaves just like a control container. You can use its Controls property to add whatever controls you like to the marker's InfoWindow content.
- Check out the online video
- StreetView support added
- IsStreetView property to switch the GoogleMap control to StreetView;
- StreetViewMode property to switch between Panorama and Overlay mode
- StreetViewPanoID property to point to a DOM element by ID, where StreetView to be shown on map click when Overlay mode is selected, if this is not set, the control will render out an DIV to be use just bellow the google map;
- Check out this online sample page StreetView
- GoogleMapView is extended with the new map types: Satellite3D, MapMakerNormal, MapMakerHybrid
Changes
(For a full list check out GoogleMap Control 4.0 beta as well)
- JSON improvements and improved Markers persistence during the post back
- InfoWindowContent property of GoogleMarker was renamed to InfoWindowTemplate
- ImageUrl property of GoogleMarker was renamed to IconUrl;
ASP.NET 2.0 Issues
- SampleWebSite2.0 was added for running the GoogleMap control samples under ASP.NET 2.0
- MarkerStyle inner property of GoogleMap control is now available to master in more general way the "Look&Feel" of all the markers.
Generic extension method for a safe item value fetch from HttpSessionState.
Hi there,
Here is an extension method for getting a value from the session in a safe manner.
1: static public bool TryGet<T>(this HttpSessionState session, string key, out T value) {
2:3: bool flag = false;
4: value = default(T);
5:6: if (session != null) {
7: object objectValue = session[key];
8: if (flag = (objectValue != null && objectValue is T)) value = (T)objectValue;
9: }10:11: return flag;
12: }The usage is simple like:
1: bool flag;
2: if(this.Page.Session.TryGet<bool>("MyFlag", out flag)) {
3: // TODO value exists and can be used
4: }Regards
GoogleMap Control 4.1 beta released
I just released GoogleMap Control 4.1 beta.
Get it from codeplex, and give it a try.Release Notes
In this release were fixed some important issues like: large number of markers using geocoding, zoom end event cycling etc.
Some new features were implemented as well, check out the list below.
I would like to highlight the proper implementation of Marker Manager coming in this release.
Check out the list of changes connected with that feature below.Issues
- Large number of markers using geocoding
- ZoomEnd event subscribing problems
Features
- MarkerManager - MarkerManager is used to manage visibility of hundreds of markers on a map, based on the map's current viewport and zoom level.
- The GoogleMaps API GMarkerManager class is deprecated and I have used the recommended open sourced MarkerManager instead.
- I have added a new inner property MarkerManagerOptions in order to manager the options for the marker manager.
- GoogleMarker class was changed and couple of new properties were added to it to: MaxZoom and MinZoom.
- A sample page was added to Samples Website under /marker/MarkerManager.aspx. Check the sample page online here
- Check out the short demo video (coming soon)
- Map
- Directions
- Polygon
Changes
- The Anonymous JS classes' functions were changed to Pseudo-Named functions for better JS debug and error feedback experience.
- Samples Websites for .NET 2.0 and VB were removed - Sorry, guys, I just have no enough time to support and keep them up to date.
Regards
GoogleMap Control using MarkerManager video
Hi, I have published a new short “HowTo” video about how to use MarkerManager with GoogleMap Control 4.1.
Check it out here.The background music is a track of the new upcoming album of my friend Svetlin Staykov – Slow Project.
I hope you enjoy both, video and music.Regards
Extension Methods: HtmlTextWriter part 1 - RenderTag
So, for me case is clear guys: “Static helper methods are dead, long live the Extension Methods.”
Since extension methods were out, I become a big fan of them.
For example, instead of:StringHelper.Encode(“Some secret”);
it looks much more nice and natural to me:
”Some secret”.Encode();
I have implemented and I’m using tons of extension methods so far and I decided to start a category in my blog where to share some of them with you guys.
Hope all those methods to be useful to you too, as they are to me.First of the series will be a couple of extension methods to HtmlTextWriter.
You know, as a web and especially web controls developer, I use it much and that’s why I like extending it.During my development I found a lot of lines where I just render and open tag and then immediately the closing (end) tag is rendered, like:
writer.AddAttribute(HtmlTextWriterAttribute.Id, fieldName + "_Error");writer.AddAttribute(HtmlTextWriterAttribute.Class, "field-validation-error");writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.RenderEndTag();
The other case I often found myself using is: open tag, write some content text and closing the tag, like:
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.Write("allow editing");writer.RenderEndTag();
I have developed some snippets for Visual Studio, which you could find at my website, in order to speed up typing such a kind of code bocks, but at some point I said to myself: why not merge those lines in a single methods. Of course, in a single extension method
.Here are the simple extension methods I came with:
/// <summary>/// Renders the tag./// </summary>/// <param name="writer">The writer.</param>/// <param name="tag">The tag.</param>public static void RenderTag(this HtmlTextWriter writer, HtmlTextWriterTag tag) {
writer.RenderBeginTag(tag);
writer.RenderEndTag();
}
/// <summary>/// Renders the tag./// </summary>/// <param name="writer">The writer.</param>/// <param name="tag">The tag.</param>public static void RenderTag(this HtmlTextWriter writer, string tag) {
writer.RenderBeginTag(tag);
writer.RenderEndTag();
}
/// <summary>/// Renders the tag./// </summary>/// <param name="writer">The writer.</param>/// <param name="tag">The tag.</param>/// <param name="content">The content.</param>public static void RenderTag(this HtmlTextWriter writer, HtmlTextWriterTag tag, string content) {
writer.RenderBeginTag(tag);
writer.Write(content);
writer.RenderEndTag();
}
/// <summary>/// Renders the tag./// </summary>/// <param name="writer">The writer.</param>/// <param name="tag">The tag.</param>/// <param name="content">The content.</param>public static void RenderTag(this HtmlTextWriter writer, string tag, string content) {
writer.RenderBeginTag(tag);
writer.Write(content);
writer.RenderEndTag();
}
Now using those extension methods the sample lines of code above will become:
writer.AddAttribute(HtmlTextWriterAttribute.Id, fieldName + "_Error");writer.AddAttribute(HtmlTextWriterAttribute.Class, "field-validation-error");writer.RenderTag(HtmlTextWriterTag.Span);
and:
writer.RenderTag(HtmlTextWriterTag.Span, "allow editing");
Hope this helps.Regards,
VelioXML Providers 4.0beta under Tiny Providers released
Today I released XML Providers 4.0beta under Tiny Membership Providers project.
Custom ASP.NET XML membership providers targeting .NET 4.0
The implementation ASP.NET XML membership providers targeting .NET 2.0 could be found at ASP.NET XmlProviders
I start with version 4.0 by purpose.
The versions bellow 4.0 are kept for future releases of .NET 2.0 implementation of ASP.NET XML membership providers.For how to use samples, please, check out the SampleWebSite under downloads.
Some more samples and screencast will be provided as possible.ASP.NET XmlProviders
Implementation of XML ASP.NET Providers (XmlRoleProvider, XmlMembershipProvider and XmlProfileProvider). This AspNet Xml Providers pack is an evolution of examples, articles and samples of code which I have packed and improved.