I'm trying to add a route that will transfer all sitemap.xml
requests to a custom request handler I made.
I tried using the following code:
routes.Add(new Route("sitemap.xml", new Helpers.SiteMapRouteHandler()));
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
But when I make a link using Url.Action()
:
Url.Action("Index", new { controller = "About"})
I get the following when I try to navigate to the XML file:
/si开发者_JS百科temap.xml?action=Index&controller=About
What am I doing wrong?
ANSWER:
I used this solution:
Specifying exact path for my ASP.NET Http Handler
If you want to route to and action instead to a request handler
You could add a route like this
routes.MapRoute(
"Sitemap",
"sitemap.xml",
new { controller = "Home", action = "SiteMap" }
);
as mentioned here - MVC: How to route /sitemap.xml to an ActionResult? and it worked for me
Update: Also ensure that <modules runAllManagedModulesForAllRequests="true">
is set.
I'm not sure if this will solve the problem, but it's worth a try:
routes.Add(
new Route(
"{request}",
new RouteValueDictionary(new { id = "" }), // this might be the tricky part to change
new RouteValueDictionary(new { request = "sitemap.xml" }),
new Helpers.SiteMapRouteHandler()
));
You have to add a handler to web.config file.
<add name="SitemapFileHandler" path="sitemap.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
And configure your routes:
routes.RouteExistingFiles = true;
Read more here: http://weblogs.asp.net/jongalloway//asp-net-mvc-routing-intercepting-file-requests-like-index-html-and-what-it-teaches-about-how-routing-works
精彩评论