views:

712

answers:

6

Hi,

I'm trying to generate a sitemap.xml on the fly for a particular asp.net website.

I found a couple solutions:

  1. chinookwebs
  2. cervoproject
  3. newtonking

Chinookwebs is working great but seems a bit inactive right now and it's impossible to personalize the "priority" and the "changefreq" tags of each and every page, they all inherit the same value from the config file.

What solutions do you guys use?

Thanks a lot for support!

A: 

Custom handler to generate the sitemap.

Nick Berardi
A: 

Using ASP.NET MVC just whipped up a quick bit of code using the .NET XML generation library and then just passed that to a view page that had an XML control on it. In the code-behind I tied the control with the ViewData. This seemed to override the default behaviour of view pages to present a different header.

Graphain
A: 

@Nick

A handler would take care of every http request to the app and regenerate on the fly the sitemap.xml, is that what you mean?

jdecuyper
+6  A: 

Usually you'll use an HTTP Handler for this. Given a request for...

http://www.yoursite.com/sitemap.axd

...your handler will respond with a formatted XML sitemap. Whether that sitemap is generated on the fly, from a database, or some other method is up to the HTTP Handler implementation.

Here's roughly what it would look like:

void IHttpHandler.ProcessRequest(HttpContext context)
{
    //
    // Important to return qualified XML (text/xml) for sitemaps
    //
    context.Response.ClearHeaders();
    context.Response.ClearContent();
    context.Response.ContentType = "text/xml";
    //
    // Create an XML writer
    //
    XmlTextWriter writer = new XmlTextWriter(context.Response.Output);
    writer.WriteStartDocument();
    writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
    //
    // Now add entries for individual pages..
    //
    writer.WriteStartElement("url");
    writer.WriteElementString("loc", "http://www.codingthewheel.com");
    // use W3 date format..
    writer.WriteElementString("lastmod", postDate.ToString("yyyy-MM-dd"));
    writer.WriteElementString("changefreq", "daily");
    writer.WriteElementString("priority", "1.0");
    writer.WriteEndElement();
    //
    // Close everything out and go home.
    //
    result.WriteEndElement();
    result.WriteEndDocument();
    writer.Flush();
}

This code can be improved but that's the basic idea.

James D
A: 

I know this isn't in ASP, but I wrote an answer to this in PHP a couple of days ago: http://www.bradshawenterprises.com/blog/2008/09/13/writing-dynamic-xml-sitemaps-using-php/

Basically I'm not bothering to use any XML parsing stuff because it's a simple file, I'm just manually typing each tag and filling the content from a database.

Have a read and it should make sense!

Rich Bradshaw
A: 

I just hit an aspx page, which generates the sitemap.xml file and also submits it to Google, Yahoo!, Bing, and Ask.

Here is a more extensive example of the details.

How-to Generate and Submit Your Sitemap to Google, Yahoo!, Bing, and Ask

damstr8