MVC: How to route /sitemap.xml to an ActionResult?

HaakonL picture HaakonL · Jan 5, 2010 · Viewed 14.2k times · Source

I've got a SitemapActionResult that overrides the ActionResult, and delivers a SEO sitemap.xml when http://www.sprelle.no/Home/SiteMap is hit. So far so good.

What I would like, though, is to serve the sitemap.xml when Google visits /sitemap.xml. For that to work, I need a route that sees "sitemap.xml" and directs to /Home/Sitemap.

How do I create this mapping (in the Routes table)?

Answer

bendewey picture bendewey · Jan 5, 2010

Add a map for:

routes.MapRoute(
            "Sitemap",
            "sitemap.xml",
            new { controller = "Home", action = "SiteMap" }
            );

Notice that the route, controller, and action options are hard coded.