简体   繁体   中英

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

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.

在此处输入图片说明

    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

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM