Search code examples
asp.netasp.net-mvcroutesweb-configasp.net-mvc-routing

How to add extension .html in url asp.net mvc 4?


I have the url: http://localhost:1714/Message/Index

I want to show: http://localhost:1714/Message/Index.html

How can I do it?


Solution

  • You need to modify Web.config to map requests for your HTML files to TransferRequestHandler.

    like so:

    <system.webServer>
        ...
        <handlers>
          <add name="HtmlFileHandler" path="*.html" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
        ...
      </system.webServer>
    

    This is explained here by Jon Galloway.

    And put this to your RouteConfig:

    public static void RegisterRoutes(RouteCollection routes)
            {
                ...
                routes.MapRoute("Default", "{controller}/{action}.html", new { controller = "Home", action = "Index" });
                ...
            }
    

    Than accessing http://localhost:{port}/Home/Index.html will send you to your Home page.