Search code examples
asp.net-mvcrouteshttp-status-code-404actionlink

Why doesn't Default route work using Html.ActionLink in this case?


I have a rather peculiar issue with routing.

Coming back to routing after not having to worry about configuration for it for a year, I am using the default route and ignore route for resources:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
        "Default",
        // Route name
        "{controller}/{action}/{id}",

        // URL with parameters
        new
        {
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        });

I have a RulesController with an action for Index and Lorem and a Index.aspx, Lorem.aspx in Views > Rules directory.

I have an ActionLink aimed at Rules/Index on the maseter page:

<li><div><%: Html.ActionLink("linkText", "Index", "Rules")%></div></li>

The link is being rendered as http://localhost:12345/Rules/ and am getting a 404.

When I type Index into the URL the application routes it to the action.

When I change the default route action from "Index" to "Lorem", the action link is being rendered as http://localhost:12345/Rules/Index adding the Index as it's no longer on the default route and the application routes to the Index action correctly.

I have used Phil Haack's Routing Debugger, but entering the url http://localhost:12345/Rules/ is causing a 404 using that too.

I think I've covered all of the rookie mistakes, relevant SO questions and basic RTFMs.

I'm assuming that "Rules" isn't any sort of reserved word in routing. Other than updating the Routes and debuugging them, what can I look at?


Solution

  • Make sure there is not a folder called 'Rules' in the same directory as your website. In its default configuration, ASP.NET MVC routes will respect physical paths before route definitions. If there is a route defined which matches the path to a physical folder in the website, the routing engine will be bypassed completely.

    You can disable routing to physical paths by changing the RouteTable.Routes.RouteExistingFiles property to false, but if you do this and your application has paths to physical resources (such as images, scripts, stylesheets, etc) you will need to accommodate for those paths with matching IgnoreRoute() definitions. For example: RouteTable.Routes.IgnoreRoute("content/{*pathInfo}");.