What is the right way to add 'app.MapControllerRoute' in program.cs of a Scaffolded Area in ASP.NET 6?
Asked Answered
K

1

5

I am creating an Area using Scaffolding for my project. As there is no startup.cs file is ASP.NET 6, I suppose I have to add it in the program.cs file. What is the right way to do that and is it any of the followings:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
      name: "Admin",
      pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
    endpoints.MapControllerRoute(
      name: "default",
      pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
})

OR

app.MapControllerRoute(
   name: "Admin",
   pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
   name: "default",
   pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
Knotgrass answered 30/1, 2022 at 15:11 Comment(0)
P
9
app.MapControllerRoute(
   name: "Admin",
   pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

OR

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
      name: "Admin",
      pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
    endpoints.MapControllerRoute(
      name: "default",
      pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
});

Both the above methods are correct in Asp.net 6 application, you can use one of them.

After configuring the areas route and creating the controller in the areas, please remember to add the [Area] attribute to the controller. like this:

enter image description here

More detail information about routing, see Areas in ASP.NET Core.

Plainspoken answered 31/1, 2022 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.