1

I want to redirect each call on a website made for www.domain.com/sitemap.xml to a controller action. How can I achieve this?

I've got this till so far, but the statuscode returned is an 302. I want to return an 200 status with it, but still redirect / rewrite to an controller action. The reason i'm asking is that I want each call for the mentioned url redirected to a controlleraction. The controller does some foo en generates an xml sitemap. Then the output of this controlleraction needs to be returned.

protected void Application_BeginRequest()
{
    //Check if an call for the sitemap.xml has been made
    if (Request.Path == "/sitemap.xml")
    {
        Response.RedirectToRoute("XmlSitemap");
    }
}
Rob
  • 6,731
  • 12
  • 52
  • 90

1 Answers1

4

You can use MVC's routing feature to achieve this:

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

With this placed placed in your global.asax file, all requests to domain.com/sitemap.xml will be routed to the Home controller's Sitemap action.

cwharris
  • 17,835
  • 4
  • 44
  • 64
  • Stupid me, the routing can already catch the url and redirect it to the right controller. Was creating unnecessary difficulties for my own this way. Thanks! – Rob Apr 16 '11 at 15:46
  • Happens to everyone in one form or another. :) – cwharris Apr 16 '11 at 15:47
  • But the second time, generated sitemap.xml is called. How can I force to recreate new sitemap? – Azarsa Mar 02 '15 at 15:11