1

I have an MVC application and am using the standard routeconfig that routes /Controller/Action/Id

I want it to additionally capture /Controller/Action.html as the url and as well and point to /controller/action also.

I am using a jquery library that I have no control over, and a function requires a url that points to a webpage or an image. However, it doesn't appear to understand that ends without an extension(.html, .php etc) is a link to html and throws an error.

Edit: I tried as the commenter below suggested, and still can't seem to get it to work. Here is my route config.

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("routeWithHtmlExtension",
            "{controller}/{action}.html",
            new { controller = "Home", action = "Index" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

This Url works:

 http://localhost:14418/Album/Test

This one does not:

 http://localhost:14418/Album/Test.html
George Stocker
  • 57,289
  • 29
  • 176
  • 237
Kyle
  • 32,731
  • 39
  • 134
  • 184
  • http://stackoverflow.com/questions/9331516/asp-net-mvc-routing-add-html-extension-to-routes – Jo Smo Nov 03 '15 at 23:43

2 Answers2

2

In web.config

<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
...
</system.webServer>
Phonix
  • 2,010
  • 18
  • 27
1

If you set up the following route, it will work:

routes.MapRoute("routeWithHtmlExtension",
    "{controller}/{action}.html",
    new {controller = "Home", action = "Index" }
);
George Stocker
  • 57,289
  • 29
  • 176
  • 237
  • 1
    I understand how the default routing works, first goes to the controller then finds the matching action with matching params. I can get to /Home/Index from a default project, but after adding the code snippet above, I cant get to /Home/Index.html =/. – Kyle Jan 24 '13 at 03:30
  • @user1308743 I'd install Phil Haack's Routedebugger and debug it if I were you. http://stackoverflow.com/questions/1945027/any-url-routing-debugger-in-asp-net-mvc I've tested the solution in a stock ASP.NET MVC project version 4 with the default 'internet application' set up, so I know it works. – George Stocker Jan 24 '13 at 13:12