MVC: How to route /sitemap.xml to an ActionResult?
I've got a SitemapActionResult that overrides the ActionResult, and delivers a SEO sitemap.xml when http://www.sprelle.no/Home/SiteMap is hit. So far so good.
What I woul开发者_C百科d like, though, is to serve the sitemap.xml when Google visits /sitemap.xml. For that to work, I need a route that sees "sitemap.xml" and directs to /Home/Sitemap.
How do I create this mapping (in the Routes table)?
Add a map for:
routes.MapRoute(
"Sitemap",
"sitemap.xml",
new { controller = "Home", action = "SiteMap" }
);
Notice that the route, controller, and action options are hard coded.
You can used this.
Step 1. Mapping the file extension to TransferRequestHandler
IIS 7 Integrated mode uses HTTP Handler mappings which point path / verb combinations to an HTTP Handler. For example, there's a default handler mapping which points path="*.axd" verb="GET,HEAD,POST,DEBUG" to the appropriate ISAPI module for the .NET runtime version the site's running under. The easiest way to see the default handlers under IIS Express is to run a site under IIS Express, right-click the IIS Express icon in the system tray, click "show all applications", and click on a site. The applicationhost.config link at the bottom is linked, so you can just click on it and it should load in Visual Studio.
If you scroll to the bottom, you'll see that there's a catchall StaticFile mapping for path="*" verb="*"
which points to StaticFileModule,DefaultDocumentModule,DirectoryListingModule
. That's what will handle your .html request if you do nothing. So the first step is to add a handler in your web.config that will point *.html
requests to the TransferRequestHandler
. TransferRequestHandler
is the handler that takes care of the extensionless URLs you're used to seeing in MVC routes, e.g. /store/details/5
.
Adding a handler mapping is really easy - just open up your web.config and add it to the <system.webServer/handlers>
node.
<add name="HtmlFileHandler" path="*.html" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
Note that you can make the path more specific if you'd like. For instance, if you only wanted to intercept one specific request, you could use path="sample.html"
Step 2. Configuring the route
Next, you'll need a new route. Open up App_Start/RouteConfig.cs
and it to the RegisterRoutes
call. My complete RegisterRoutes
looks like this:
routes.MapRoute(
name: "XMLPath",
url: "sitemapindex.xml",
defaults: new { controller = "Home", action = "Html", page = UrlParameter.Optional }
);
Step 3. Route Existing Files
That almost covers it, but there's one more thing to take care of - overriding requests that match an existing file. If you've got an actual file called myfile.html, the routing system won't allow your route to run. I forgot about this, ended up with an HTTP 500 error (recursion overflow) and had to ask Eilon Lipton for help.
Anyways, that's easy to fix - just add routes.RouteExistingFiles = true to your route registration. My completed RegisterRoutes call looks like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.RouteExistingFiles = true;
routes.MapRoute(
name: "CrazyPants",
url: "{page}.html",
defaults: new { controller = "Home", action = "Html", page = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
That's it.
I tested by adding this controller action:
public FileResult Html()
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
stringBuilder.AppendLine("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
stringBuilder.AppendLine("<sitemap>");
stringBuilder.AppendLine("<loc>http://sprint-newhomes.move.com/sitemaps/sitemap_01.xml</loc>");
stringBuilder.AppendLine("<lastmod>" + DateTime.Now.ToString("MMMM-dd-yyyy HH:mm:ss tt") + "</lastmod>");
stringBuilder.AppendLine("</sitemap>");
stringBuilder.AppendLine("<sitemap>");
stringBuilder.AppendLine("<loc>http://sprint-newhomes.move.com/sitemaps/sitemap_02.xml</loc>");
stringBuilder.AppendLine("<lastmod>" + DateTime.Now.ToString("MMMM-dd-yyyy HH:mm:ss tt") + "</lastmod>");
stringBuilder.AppendLine("</sitemap>");
stringBuilder.AppendLine("</sitemapindex>");
var ms = new MemoryStream(Encoding.ASCII.GetBytes(stringBuilder.ToString()));
Response.AppendHeader("Content-Disposition", "inline;filename=sitemapindex.xml");
return new FileStreamResult(ms, "text/xml");
}
To get this to work you need to do 2 things:
- Instruct the IIS to allow the static file request "/sitemap.xml" to hit your controller. Otherwise the IIS will bypass your application and look directly for a file with this name. Add the following line to your web.config:
<system.webServer>
<handlers>
<!-- add the following line -->
<add name="SitemapXml" path="sitemap.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
</system.webServer>
- Place a route in your MVC application that matches this request to an ActionResult (make sure you place it before your default route):
routes.MapRoute(
name: "Sitemap",
url: "sitemap.xml",
defaults: new { controller = "YourControllerName", action = "YourActionName" }
);
精彩评论