Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Areas - Non Area Route Resolves To Area

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.

like image 789
Tim Scott Avatar asked Apr 17 '11 19:04

Tim Scott


People also ask

What are the three segments for routing important in MVC?

The three segments of a default route contain the Controller, Action and Id.

How do areas work in MVC?

Areas allows you to separate your modules and organize Model, View, Controller, Web. config and Routing registration file into separate sections. In live MVC Project implementation we can use Areas concept for organizing project in better manageable way. Area separate logical section like Model, View, Controller, Web.

Which is the correct default route mapping within MVC?

The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id. The Default route maps this URL to the following parameters: controller = Home. action = Index.

What is area routing?

The routing area, abbreviated as RA, is the counterpart of the location area (LA) in packet-switched (PA) networks. The RA is usually a smaller area compared to the LA because using multimedia services requires more frequent paging messages.


1 Answers

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

like image 94
Łukasz W. Avatar answered Sep 21 '22 19:09

Łukasz W.