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
1

2 Answers 2

2

In web.config

<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
...
</system.webServer>
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

2 Comments

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 =/.
@user1308743 I'd install Phil Haack's Routedebugger and debug it if I were you. stackoverflow.com/questions/1945027/… 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.