I am using areas in my ASP.NET Core 3.1 application (MVC).
Now I want all requests without an explicit area to go to the "Main" area by default. This is how I currently set up my endpoint routing:
app.UseEndpoints(endpoints =>
{
// 1
endpoints.MapControllerRoute(
name: "area",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
// 2
endpoints.MapAreaControllerRoute(
name: "default",
areaName: "Main",
pattern: "{area=Main}/{controller=Home}/{action=Index}/{id?}");
});
My goal is:
If the request URL contains an existing area name, use routing [1]. If there is no area name, use routing [2] (which defaults to the "Main" area).
My problem:
How do I set up the default area?
OK, solved. In the end, this here has been working for me:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapAreaControllerRoute(
name: "default",
areaName: "Main",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
UseEndpoints(endpoints => { endpoints. MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Inside the call to UseEndpoints() , we use the MapControllerRoute() method to create a route by giving the name default .
An area is effectively a structure inside an app. In an ASP.NET Core web project, logical components like Pages, Model, Controller, and View are kept in different folders. The ASP.NET Core runtime uses naming conventions to create the relationship between these components.
To add a new Area, Right Click on application name from solution explorer -> Select Add -> Select New Scaffolded Item -> select MVC Area from middle pane of dialog box -> Enter Name of Area -> Click Ok.
MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); The route names give the route a logical name. The named route can be used for URL generation. Using a named route simplifies URL creation when the ordering of routes could make URL generation complicated.
Try to use below routing configuration:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
endpoints.MapAreaControllerRoute(
name: "Main",
areaName: "Main",
pattern: "{controller=Home}/{action=Index}/{id?}"
);
});
There are 2 ways:
Controller
and Action
outside the Areas. Besides,
The Important Area should be outside Areas
to make it as normal (Default Area) as you wish. app.UseEndpoints(endpoints =>
{
// 1
endpoints.MapControllerRoute(
name: "area",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
// 2
endpoints.MapAreaControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
{area=Main}/
in your patternapp.UseMvc(routes =>
{
routes.MapControllerRoute(
name: "area",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
routes.MapAreaControllerRoute(
name: "default",
areaName: "Main",
template: "{controller=Home}/{action=Index}/{id?}");
});
Refer the following thread to have a better understanding
ASP.NET Core 2 default route having areas
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With