0

I'm having an issue with routing and areas as the title says. I have ana areas section in my project that just has one area in it. It has a Policy controller in there. Outside in the project there is a controller's folder with a LoginController.

When I try to got to the URL "BoB/Login/ChangeSection" (BoB is project name, Login is controller, ChangeSection is action) it gets sent to "BoB/Policy/Login". This means the Area registration inside the Policy area is grabbing the request and putting the Policy prefix in front when it shouldn't be.

Here is my project RouteConfig

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "BoBHome", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "BoB.Controllers" }
            );

and here is the Area registration

public override void RegisterArea(AreaRegistrationContext context)
    {  
        context.MapRoute(
            name: "BoBPolicy_default",
            url: "Policy/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "BoB.Areas.BoBPolicy.Controllers" }
        );
    }

I was under the impression the namespaces would keep this from happening, but it obviously is not. Any help is appreciated.

Some additional info:

This comes from another project that is not MVC so the Redirect looks like this:

Response.Redirect("/BoB/BoBLogin/ChangeSection?controller=PolicyOverview");
HarvP
  • 190
  • 3
  • 13

1 Answers1

1

I think your issue is similar to non area route issue, which requires usage of area parameter to prevent area URL prefix inserted when using default route:

public override void RegisterArea(AreaRegistrationContext context)
{  
    // set area parameter with area name
    context.MapRoute(
        name: "BoBPolicy_default",
        url: "Policy/{controller}/{action}/{id}",
        // add area parameter here
        defaults: new { area = "Policy", controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new string[] { "BoB.Areas.BoBPolicy.Controllers" }
    );
}

NB: Since area route registration always placed on top of default route (read this answer for reasons behind it), it is possible to have routing engine incorrectly picking up default route request into area route when area route constraint(s) not specified.

Related:

MVC5 Routing Go to specific Areas view when root url of site is entered

Community
  • 1
  • 1
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61