For a SPA application I’m building I need to accept requests to .html files although I didn’t use static html files but razor views instead. To get this working inside ASP.NET MVC is a little bit harder than you would expect. So here are the required steps:
- Add a handler mapping in your web.config that will point *.html requests to the TransferRequestHandler. Therefore add the following section to the<system.webServer> node:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<handlers> | |
<add name="HtmlFileHandler" path="*.html" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/> | |
</handlers> |
-
Add a custom route. Open up App_Start/RouteConfig.cs and it to the RegisterRoutes call:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
routes.MapRoute( | |
name: "Durandal App Views", | |
url: "App/features/{viewName}.html", | |
defaults: new { controller = "DurandalView", action = "Get" } | |
); |