I have a mvc application Areas folder structure of the following type:
Areas => FolderA => Controllers => ControllerA & ControllerB
I have used the following register path in AreaRegistration:
context.MapRoute(
"default1",
"FolderA/{controller}/{action}/{id}",
new { controller = "ControllerA|ControllerB", action = "Index", id = UrlParameter.Optional }
);
and I have two links on a shared layout as:
@Html.ActionLink("Link 1", "ActionA", "ControllerA", null)
@Html.ActionLink("Link 2", "ActionB", "ControllerB", null)
Link 1 seems to be working fine and redirects as expected. Issue is with Link2, which always forms the following url and i get 404 error.
http://localhost:29661/FolderA/ControllerA/ActionB?Length=15
The default application route path is:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Seems like it is always looking for ActionB in the same controller, even i have 2 different paths registered. Can anyone please help it out.
ControllerA|ControllerB
is not a valid default value. I believe what you want is a constraint, not a default for your controller. So, your route should look like this instead:
context.MapRoute(
"default1",
"FolderA/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "ControllerA|ControllerB" }
);
But in this case you don't need a constraint because the route to use can be determined by the area name, so you could get by with:
context.MapRoute(
"default1",
"FolderA/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
The default you use for controller will be the location it navigates to if there is no controller provided in the URL. You would either use a controller in your area as the default or exclude the default controller to require there to be a controller in the URL.
In any case, you need to provide the area name when building the link to an area. The overload that you are using will not work when you provide a string as the 3rd parameter.
@Html.ActionLink("Link 1", "ActionA", "ControllerA", new { area = "FolderA" }, null)
@Html.ActionLink("Link 2", "ActionB", "ControllerB", new { area = "FolderA" }, null)
And as pointed out here, you need to specify the area even when you want to navigate to a non-area link.
@Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)