9

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?}");
 });
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
Ingmar
  • 1,525
  • 6
  • 34
  • 51

2 Answers2

7

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

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
  • Thanks for your help, Phong. Your lower snippet does not compile. And the upper leads to a 404 still. I thought, I need to start with the most specific route and the proceed with more general routes. In your snippets, I feel like you doing the opposite, no? – Ingmar Mar 04 '20 at 08:59
  • How do you specify the routing to get `Controller` and `Action` outside `Area`. That's my point. https://ibb.co/hcF1XMn – Nguyễn Văn Phong Mar 04 '20 at 09:01
  • Hm. Well I have no controller outside from areas. So everything I have is wrapped in areas.However, there is one area that is more important than all my other areas. And my goal is to route to this important area if there is no area in url. – Ingmar Mar 04 '20 at 09:07
  • Obviously, the **Important Area** should be outside to make it as normal (Default Area). – Nguyễn Văn Phong Mar 04 '20 at 09:08
  • 1
    Haha, yes. But I don't want to change everything now. Thanks Phong. Still looking for a way to make it work my way. – Ingmar Mar 04 '20 at 09:16
  • Did you try this one? https://stackoverflow.com/questions/48368557/asp-net-core-2-default-route-having-areas – Nguyễn Văn Phong Mar 04 '20 at 09:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208969/discussion-between-phong-and-ingmar). – Nguyễn Văn Phong Mar 04 '20 at 09:27
1

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

});
Ryan
  • 19,118
  • 10
  • 37
  • 53
  • Thanks @Xing Zou, but this didn't do the trick. Requests to "/admin" still result in a 404. – Ingmar Mar 03 '20 at 10:40