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

Modifications

Created the write posts function, seems very simple.

private void WritePosts(PostCollection posts, XmlTextWriter writer)
{
  foreach(Post p in posts)
    {
       writer.WriteStartElement("url");
       writer.WriteElementString("loc", 
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) )
	{
	if(bool.Parse(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

Download the GSiteMap.ashx file

Print | posted on Wednesday, March 01, 2006 11:45 PM

&uot&uot

Comments on this post

# GSiteMap

Requesting Gravatar...
Here's the link to the output file
http://www.kowitz.net/GSiteMap.ashx
Left by Brendan on Mar 08, 2006 8:27 PM

# Thanks for that

Requesting Gravatar...
Generate Google sitemap with .net, that was exactly what I was looking for.
Left by ???? on Aug 23, 2006 6:47 AM

Your comment:

 (will show your gravatar)
 
Please add 5 and 4 and type the answer here: