简体   繁体   中英

C# mvc3 redirect sitemap.xml to controller action

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");
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM