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