How to use an Area in ASP.NET Core

2020-01-27 12:08发布

How do I use an Area in ASP.NET Core?

I have an app that needs an Admin section. This section requires its Views to be placed in that area. All requests that start with Admin/ will need to be redirected to that area.

8条回答
在下西门庆
2楼-- · 2020-01-27 12:29

Areas Implementation in Routing First Create Area(Admin) using VS and add the following code into Startup.cs First Way to Implement:- Add Controller Login and Index Action and add Following Code, [Area(“Admin”)] is compulsory to add on controller level to perform asp.net areas Routing. Startup.cs

 app.UseMvc(routes =>
            {
                routes.MapRoute(
                  name: "areas",
                  template: "{area:exists}/{controller=Login}/{action=Index}/{id?}"
                );
            });

Note: Area routing must be placed first with non area routing, area: exists is compulsory to add area routing.

Controller Code:

[Area("Admin")] 
    public class LoginController : Controller
    {
        public IActionResult Index()
        {
            return Content("Area Admin Login Controller and Index Action");
        }
    }

This route may be called using http://localhost:111/Admin

Second Way to Implement Area Routing:- Add Following code into startup.cs.

app.UseMvc(routes =>
            {
                routes.MapAreaRoute(
    name: "default",
    areaName: "Guest",
    template: "Guest/{controller}/{action}/{id?}",
    defaults: new { controller = "GuestLogin", action = "Index" });
            });

Create an Area “Guest”, Add “GuestLogin” Controller and “Index” Action and add the following code into the newly created controller.

[Area("Guest")]
    public class GuestLoginController : Controller
    {
        public IActionResult Index()
        {
            return Content("Area Guest Login Controller and Index Action");
        }
    }

This route may be called using http://localhost:111/Guest

查看更多
Lonely孤独者°
3楼-- · 2020-01-27 12:29

In the Microsoft docs to migrate from ASP.NET CORE 2.2 to 3.0 the suggestion is to:

Replace UseMvc with UseEndpoints.

I encountered some challenges while trying to fix my Area's while simultaneously having Identity to keep working - but the solution below seems to be working for ASP.NET CORE 3.0 :

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

Hopefully I could also help you out and reduce the research time :-)

查看更多
唯我独甜
4楼-- · 2020-01-27 12:32

Use this pattern in Configure method in Startup.Cs, as its full routing manner:

app.UseMvc(routes =>{
   routes.MapRoute(
   name: "MyArea",
   template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}/{id?}");});

In Core 3.1 you should use below code in ConfigureServices method:

services.AddMvc(option => option.EnableEndpointRouting = false);
查看更多
太酷不给撩
5楼-- · 2020-01-27 12:43

In ASP.NET Core 3.0. If you are working with Endpoint patterns, after adding the Area (Right click over project, Add, Area), you have to add manually routing pattern on startup.cs Configure method. (At this point the generated ScaffoldingReadMe.txt is out of date).

app.UseEndpoints(endpoints =>
{

    endpoints.MapAreaControllerRoute(
        "Admin",
        "Admin",
        "Admin/{controller=Home}/{action=Index}/{id?}");

    endpoints.MapControllerRoute(
         name: "default",
         pattern: "{controller=Home}/{action=Index}/{id?}");
});
查看更多
女痞
6楼-- · 2020-01-27 12:46

Scaffolding has generated all the files and added the required dependencies.

However the Application's Startup code may required additional changes for things to work end to end. Add the following code to the Configure method in your Application's Startup class if not already done:

    app.UseMvc(routes =>
    {
      route.MapRoute(
        name : "areas",
        template : "{area:exists}/{controller=Home}/{action=Index}/{id?}"
      );
    });
查看更多
孤傲高冷的网名
7楼-- · 2020-01-27 12:46
With .net core, following is needed to be added in the startup file if you are adding an area:

     app.UseMvc(routes =>
            {
                routes.MapRoute(
                  name: "areas",
                  template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
                );
            });

After that you can just simply mark your area and route in the controller, i.e
     [Area("Order")]
     [Route("order")]

it works for me.

查看更多
登录 后发表回答