ASP.NET Core MVC : areas common functionality

Viewed 21

I am creating an ASP.NET Core MVC project with areas. Between two of these areas, there is common functionality.

As a convention is it better to have copies of the same controllers and views in each area, or should they be kept in the root folder?

1 Answers

As a convention,we will have controllers and views in each area,th structure will be like this:

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

And you need to add [Area] attribute in your controllers.Also you need to set the route with area:

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

For more details,yo can refer to the official doc.

Related