C# mvc3 redirect sitemap.xml to controller action

孤街浪徒 提交于 2019-12-08 03:09:48

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/5687471/c-sharp-mvc3-redirect-sitemap-xml-to-controller-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!