MVC3 MapRoute Ambiguous between Controller Name without area and the equal Area Name
-
28-06-2021 - |
Pergunta
This Is My Controllers in root (Without Area):
- Home
- Members
And My Areas are:
+General
- Controller1
- Controller2
+Members
- Manage
- Member
So My Login
Action is in Members Controller (in Root) before I Add Members Area every thing is fine, but know I receive 404 error for this url
(http://MyProject.dev/members/login?ReturnUrl=%2f)
So How can I define a MapRoute to fix this problem?
Update
I try this one in Main Global.asax
:
routes.MapRoute(
"newLogMaproute",
"members/login{*pathInfo}",
new { area = "", controller = "Members", action = "Login"}
);
But there is an error: A path segment that contains more than one section, such as a literal section or a parameter, cannot contain a catch-all parameter.
And I try this:
routes.MapRoute(
"newLogMaproute",
"members/login/{*pathInfo}",
new { area = "", controller = "Members", action = "Login"}
);
but this one returned 404.
Solução
What's happening here is the area registration is happening before your member route, so the area route is always taking precedence.
I fixed this in a test app by creating the following in the Global.asax:
public static void RegisterPreAreaRoutes(RouteCollection routes)
{
routes.MapRoute(
"Members", // Route name
"members/login", // URL with parameters
new { controller = "members", action = "login" } // Parameter defaults
);
}
Then in the Application_Start making sure this route gets mapped before the areas get registered:
protected void Application_Start()
{
RegisterPreAreaRoutes(RouteTable.Routes);
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters( GlobalFilters.Filters );
RegisterRoutes( RouteTable.Routes );
}
Outras dicas
You need define Custom Routes for your area. This is not good. You will probably have problems after. The best is not to create an ambiguity.
Don't forget of define the namespaces in rotes configuration with new string[]:
routes.MapRoute(
"Members", // Route name
"members/login", // URL with parameters
new { }, // Parameter defaults
new[] { "WebApp.Controllers" } // Namespaces for find
);