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 would 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)?
To get this to work you need to do 2 things:
Add a map for:
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 toStaticFileModule,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 theTransferRequestHandler
.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.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 theRegisterRoutes
call. My completeRegisterRoutes
looks like this: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:
That's it.
I tested by adding this controller action: