I have multiple numbers of Areas in me project. After upgrading project form .NET Core 2.2 to 3.1 those Areas aren't work/call.
My Controller inside Area looks like: -
[Area(nameof(CRM))]
[Authorize]
[ServiceFilter(typeof(ActionFilter))]
public class DefaultController : Controller
{
public IActionResult Index()
{
return View();
}
}
And the previous configurations for handling multiple Area looks like below: -
routes.MapRoute(name: "Area", template: "{area:exists}/{controller=Default}/{action=Index}/{id?}");
routes.MapRoute(name: "Area Default Create", template: "{area:exists}/{controller=Default}/create", defaults: new { action = "CreateOrEdit" });
routes.MapRoute(name: "Area Default Edit", template: "{area:exists}/{controller=Default}/edit/{id}", defaults: new { action = "CreateOrEdit" });
As we have to use UseEndpoints in .NET Core 3.1 and the new Configuration for handling Multiple-Area is likes below [What I was tried]
Reference # Endpoint Routing .net core 3-Multiple Areas & https://aregcode.com/blog/2019/dotnetcore-understanding-aspnet-endpoint-routing/
endpoints.MapControllerRoute(name: "areas", pattern: "{area}/{controller}/{action=Index}/{id?}");
But, It isn't works also try to call by below way & not succeed: -
endpoints.MapAreaControllerRoute(name: "Area", areaName: "areas", pattern: "{area:exists}/{controller=Default}/{action=Index}/{id?}");
endpoints.MapAreaControllerRoute(name: "Area Default Create", areaName: "areas", pattern: "{area:exists}/{controller=Default}/create", defaults: new { action = "CreateOrEdit" });
endpoints.MapAreaControllerRoute(name: "Area Default Edit", areaName: "areas", pattern: "{area:exists}/{controller=Default}/edit/{id}", defaults: new { action = "CreateOrEdit" });
Even it's not working for individual Area too
endpoints.MapAreaControllerRoute(name: "Area", areaName: "CRM", pattern: "CRM/{controller=Default}/{action=Index}/{id?}");
I just solve the problem by myself and here is the solution for overcoming this situation: -
In Startup.cs class add below line for handling dynamic route for multiple Areas
endpoints.MapAreaControllerRoute(name: "areas", "areas", pattern: "{area:exists}/{controller=Default}/{action=Index}/{id?}");
Here areas is the name of your Areas folder [Default]. You can change it as your wish.
Change the header markup of controllers likes below: -
[Area(nameof(CRM))] // CRM is the another-folder name inside Areas folder
[Route("CRM/[controller]/[action]")]
[Authorize]
[ServiceFilter(typeof(ActionFilter))]
public class DefaultController : Controller
{
public IActionResult Index()
{
return View();
}
}
Finally change your Razor markup likes below to call your target Action inside Controller of Areas folder
<a asp-area="CRM" asp-controller="Default" asp-action="Index">Home of CRM</a>