I have an ASP.NET Core project with only one area called Data
. The default Home
controller is still in no area.
The linking to the area Data
in the project does not work. The routing works fine though.
If I manually enter the link localhost/Data I get the correct action Index
of the controller Home
of the area Data
. So the routing definitely works.
However <a asp-area="Data" asp-controller="Home" asp-action="Index">Data</a>
gets evaluated to the link localhost/?area=Data, which does not work and should actually be localhost/Data.
Links within the area Data
are also not evaluating properly. What should be localhost/Data/Controller is just localhost/Controller. Again manually entering the link works fine.
How I configured the project so far to try and get the links working: - Routes are added as follows
app.UseMvc(routes =>
{
// Default Route
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}"
);
// Area Route
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
});
Data
have the attribute [Area("Data")]
._ViewImport.cshtml
in Areas/Data/Views @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
.I in general tried to follow the official documentation, but nothing appears to work.
So I obviously would like to get the links working without "hardcoding" them and am wondering why the intended behaviour does not seem to work.
Any hints are appreciated.
You need to change the order of your routes. You should start with more specific routes and then to more general routes:
app.UseMvc(routes =>
{
// More specific area route
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
// Any other (default)
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}"
);
});