Reputation: 6891
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");
}
}
Upvotes: 1
Views: 2045
Reputation: 18125
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.
Upvotes: 4