ASP.NET MVC: Route a .html request to an MVC route

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:

<modules runAllManagedModulesForAllRequests=”true”>

<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:
routes.MapRoute( 
                name: "Durandal App Views", 
                url: "App/features/{viewName}.html", 
                defaults: new { controller = "DurandalView", action = "Get" } 
            );

For MVC 5

 [Route("news/{slug}/{id}.html")]


 

Published by

tuanitpro

Tôi là Lê Thanh Tuấn, và tôi chia sẻ những điều mình cho rằng nó là thú vị, hay giúp ích cho bạn!

Leave a Reply