If we have more areas in one project, where should routing values should be stored.
What should go to Global.asax and what in class in AreaAreaRegistration.cs
I don’t use asp mvc often (it’s still hobby) but routing makes me problems all the time 🙁
I must give one example that confuse me:
I have two areas and link in one area (CityPage) that should open page in another area.
So in my global.asax I have:
routes.MapRoute(
"CityPage_home",
"{country}/{city}",
new { controller = "Home", action = "Index", country = UrlParameter.Optional, city = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { area = "CityPage", controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And this works. But if I move “*CityPage_home*” from global.asax to CityPageAreaRegistration.cs then when I click on link that should open page in another area my application crash because code enters first in CityPage/Home/Index method.
I don’t understand why this routing work in first case and not in another. Any idea?
Within in each area a file will be generated for you. For example.
AdminAreawould contain a file namedAdminAreaRegistrationWith a method like this inside:
I like to modify this by adding a default controller.
Action Links
Traditionally we use the following to create a link:
@Html.ActionLink("MyLink","Index","Home",)To create a link to the admin area we use this:
@Html.ActionLink("Admin Area","Index", new { area = "Admin", controller = "Home" })Now if you’re inside the admin and want to place a link to the home page you need to do this:
@Html.ActionLink("Home","Index", new { area="", controller = "Home })And finally, if you need to create custom routes you can put the area routes in the new file that is created within each area, and continue to put your non area specific routes in the normal Global.asax.