MVC Areas - Non Area Route Resolves To Area
Asked Answered
H

1

8

I have added an area to my MVC 3 project. I cannot seem to get routing working with a very simple scenario. It seems to always want to resolve to the area. Here is my configuration. At startup:

AreaRegistration.RegisterAllAreas();
IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Browse", action = "Index", id = UrlParameter.Optional }

And

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get { return "Admin"; }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Users", action = "Index", id = UrlParameter.Optional }
        );
    }
}

In web.config:

<authentication mode="Forms">
  <forms loginUrl="~/Login" defaultUrl="~/Browse" timeout="60" cookieless="UseDeviceProfile" />
</authentication>

I am using RouteDebugger to try to solve it. When I navigate to the Login page the debugger shows:

  • AppRelativeCurrentExecutionFilePath: ~Login
  • Admin/{controller}/{action}/{id} Does Not Match Current Request
  • {controller}/{action}/{id} Matches Current Request
  • Matched Route: {controller}/{action}/{id}

So far so good. But then it shows this:

  • Generated URL: /Admin/Login?ReturnUrl=%2F using the route "Admin/{controller}/{action}/{id}"

Next I log in. My Login/Index method is not hit, and the debugger shows:

  • AppRelativeCurrentExecutionFilePath: ~Login
  • Admin/{controller}/{action}/{id} Does Not Match Current Request
  • {controller}/{action}/{id} Matches Current Request
  • Matched Route: {controller}/{action}/{id}
  • Generated URL: /Admin/Login?ReturnUrl=%2FAdmin%2FLogin using the route "Admin/{controller}/{action}/{id}"

On the one hand it says that it does not match the Admin route, then in the generated URL it says it's using that route. I'm stumped.

Hyacinthus answered 17/4, 2011 at 19:12 Comment(0)
B
3

Try to add your area parameter with a predefined value to your routing definition... For example instead of:

context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Users", action = "Index", id = UrlParameter.Optional }
        );

use:

context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { area = "Admin", controller = "Users", action = "Index", id = UrlParameter.Optional }
        );

Let me know if it helps... Regards

Breast answered 4/5, 2011 at 12:32 Comment(3)
I did exactly as you say. First I tried doing the area registration before non area registration. No good. Links to non-area controller always resolve with "Admin/" in front of them. So I reversed the registration order. Then when I navigate to "~/Admin" I get "~/Home/NotFound" with the debugger showing a match on the "{controller}/{action}/{id}" route. When I navigate to "~/Admin/Users", I get "~/Admin/Home/Home/Home/NotFound" with the debugger showing match on the {catchall} route.Hyacinthus
When I say "Links to non-area controller always resolve with 'Admin/' in front of them" that's not totally true. Html.ActionLink<T> resolves correctly. But Html.BeginForm<T> and Html.BuildUrlFromExpression<T> resolve to the Admin area even when T is a controller that is NOT in an area.Hyacinthus
I was using the 2.0 version of Microsoft.Web.Mvc, in which these methods do not understand areas. It's fixed in 3.0.Hyacinthus

© 2022 - 2024 — McMap. All rights reserved.