Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the default area in ASP.NET Core?

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:

  • Requests to "/main/admin" are working fine.
  • Requests to "/admin" result in a 404.

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?}");
 });
like image 358
Ingmar Avatar asked Mar 02 '20 08:03

Ingmar


People also ask

How would you setup the default route using endpoints?

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 .

What is ASP area in ASP.NET Core?

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.

Can you identify the correct steps for adding area to ASP.NET Core application?

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.

How do I use MapControllerRoute?

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.


2 Answers

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?}"
    );

});
like image 42
Ryan Avatar answered Sep 20 '22 11:09

Ryan


There are 2 ways:

  1. If you do not specify the area name, it'll find the Controller and Action outside the Areas. Besides, The Important Area should be outside Areas to make it as normal (Default Area) as you wish.

enter image description here

    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?}");

    });
  1. Remove redundant {area=Main}/ in your pattern
app.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

like image 133
Nguyễn Văn Phong Avatar answered Sep 18 '22 11:09

Nguyễn Văn Phong