2

I need to generate a sitemap to validate the site with Google Webmaster Tool.

How can I generate the sitemap for my website automatically?

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
ridermansb
  • 10,779
  • 24
  • 115
  • 226

2 Answers2

3

Try this sample:

@using System.Xml.Linq;
@{
    var urls = new List<string>{"home", "about", "contact"};
    XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
    var baseurl = "http://www.domain.com/{0}";
    var sitemap = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"),
            new XElement(ns + "urlset",
                from url in urls select
                new XElement("url",
                    new XElement("loc", string.Format(baseurl, url)),
                    new XElement("lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
                    new XElement("changefreq", "monthly"),
                    new XElement("priority", "0.5")
                    )
                )
            );
    Response.ContentType = "text/xml";
    sitemap.Save(Response.Output);
}

Save the file as Sitemap.cshtml. Obviously, you will need to replace the List with a source of locations for the map. But at least you can see how the XML is generated.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
2

I think this is your best bet:

ASP.NET MVC SiteMap provider

Read these pages:

Dynamic sitemaps

Exporting the sitemap for search engine indexing

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • The site was developed in WebMatrix .. with Razor, as a provider of MVC can help? I do not understand. – ridermansb May 13 '11 at 03:48
  • @Riderman: no problem. Just register the provider and you can start using it. The documentation is right there in the homepage of the project at CodePlex. – Leniel Maccaferri May 13 '11 at 14:11