ASP.NET Google Sitemap

Probably the best thing about SUB is its simplicity. There are two things I’d like to try to document in this post, firstly is the code for adding a simple google sitemap, the other is actually a question. I’m still curios to why Darren has used hard coded URL formatting in most of the repeaters when displaying a collection of posts. Under the APP_Code/ directory there is a UrlFormatter class which can generate a URL for any ApplicationDataObject (post or feedback item).

Taking this UrlFormatter class into consideration I have modifiied the GSiteMap.ashx file include a method that will write out all the post URLs.

Heres a link to the original code: ASP.NET Google Sitemap


Created the write posts function, seems very simple.

private void WritePosts(PostCollection posts, XmlTextWriter writer)
  foreach(Post p in posts)
MarkItUp.SingleUserBlog.Web.UrlFormatter.FormatUri(p)); writer.WriteElementString("lastmod",
p.DateModified.ToUniversalTime().ToString("yyyy-MM-ddThh:mm:ss.fffZ")); writer.WriteEndElement(); // url } }

The only other modification I made was to make the XML node writer look at the “mainMenu” property. This is so it only writes out the items that are actually displayed on the menu.

string displayOnMainMenu = node["mainMenu"];
	if(!string.IsNullOrEmpty(displayOnMainMenu) )
	{ …

The bigger picture for UrlFormatter.FormatUri()

After doing this I decided to soup up the FormatUri function to allow it to ‘potentially’ format friendly URLs. So I’ve also added configuration setting to allow me to switch, friendly URLs on and off.
public static string EncodeUri(string str)
            char[] badchars = new char[] 
{ '?','!','.',',','@','#','$','%','^','&','*',
'(',')','{','}','[',']','<','>','\'' }; foreach (char c in badchars) str = str.Replace(c.ToString(), ""); return System.Web.HttpContext.Current.Server.UrlEncode(str); } public static string FormatUri(ApplicationDataObject item)
{ if (item is Post) { if (BlogConfigurationSettings.FriendlyURLs) { return string.Format("{0}ViewPost/{1}/{2}.aspx", Globals.UrlStartPath,
item.Id, EncodeUri(item.DisplayName)); } else { return string.Format("{0}Posts/Post.aspx?postId={1}",
Globals.UrlStartPath, item.Id); } }

Now the EncodeUri function has become even more vital in providing correct links to all the posts in the site. So to welcome the friendly URLs change, I’ve been changing all the repeaters to use this as the hyperlink for the posts instead.

<A href="<%# MarkItUp.SingleUserBlog.Web.UrlFormatter.FormatUri((MarkItUp.SingleUserBlog.Services.Post)Container.DataItem) %>"…</A>

So with a few small modifications, basically I can modify all the post URLs in the entire site, by only changing one function. But for now the BlogConfigurationSettings.FriendlyURLs option will remain false.


Download the GSiteMap.ashx file

.NET Code Snippets SingleUserBlog
Posted by: Brendan Kowitz
Last revised: 21 Sep 2013 12:15PM


3/9/2006 9:27:52 AM
Here's the link to the output file
8/23/2006 6:47:50 PM
Generate Google sitemap with .net, that was exactly what I was looking for.
6/25/2011 1:00:11 PM
Arthritis Dermamax Drugs - buy lunesta It works to restore the chemical imbalance caused by sleep problems. - eszopiclone without prescription
6/25/2011 5:54:08 PM
Nutmeg As A Drug - clopidogrel no prescription With Plavix these side effects can be quite serious such as very low levels of white blood cells, thrombotic thrombocytopenic purpura, hemorrhage, and gastrointestinal upset. plavix medication

No new comments are allowed on this post.