How to create an "Area" in MVC in . net core 3.1?

Asked

Viewed 154 times

-1

The path to creating an Area no longer appears in . net core 3.1

Path: Project > Right Click > Add > Area

  • Were you able to verify the answer? Do not forget to accept it if you are satisfied with it.

2 answers

1

According to the framework documentation, to create an area you need:

  • The directory structure with the areas (required for views only)
  • Controllers decorated with [Area("nome da área")]
  • Creation of the route in startup

Example of structure

Project name
  Areas
    Products
      Controllers
        ManageController.cs
      Views
        Home
          Index.cshtml
        Manage
          Index.cshtml

Example of controller

[Area("Products")]
public class ManageController : Controller
{

Setup on startup

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

    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

-3

Project > Add > New Scaffolded Item > MVC Area

Browser other questions tagged

You are not signed in. Login or sign up in order to post.