How to use an Area in ASP.NET Core

前端 未结 8 713
[愿得一人]
[愿得一人] 2020-11-28 23:45

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 request

相关标签:
8条回答
  • 2020-11-28 23:59

    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 =>
    {
        routes.MapRoute(
            name : "areas",
            template : "{area:exists}/{controller=Home}/{action=Index}/{id?}");
    });
    
    0 讨论(0)
  • 2020-11-29 00:03

    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);
    
    0 讨论(0)
  • 2020-11-29 00:05

    In ASP.NET Core 3.0. If you are working with Endpoint patterns, after adding the Area (Right click over project, Add, New Scaffolded Item, 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?}");
    });
    
    0 讨论(0)
  • 2020-11-29 00:09

    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);
    
    0 讨论(0)
  • 2020-11-29 00:14

    In order to include an Area in an ASP.NET Core app, first we need to include a conventional route in the Startup.cs file (It's best to place it before any non-area route):

    In Startup.cs/Configure method:

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

    Then make a folder named Areas in the app root and make another named Admin inside the former, also make these folders inside Admin (ViewComponent is optional):

    Now we create a controller inside the Controllers folder named AdminController, the content can be like:

    [Area("Admin")]
    [Route("admin")]
    public class AdminController : Controller
    {
        public AdminController()
        {
            // do stuff
        }
    
        public IActionResult Index()
        {
            return View();
        }
    
        [Route("[action]/{page:int?}")]
        public IActionResult Orders()
        {
            return View();
        }
    
        [Route("[action]")]
        public IActionResult Shop()
        {
            return View();
        }
    
        [Route("[action]/newest")]
        public IActionResult Payments()
        {
            return View();
        }
    }
    

    Now in order for that to work, you'll need to create Views for all actions that return one. The hierarchy for views is just like what you have in a non-area Views folder:

    Now, you should be good to go!

    Question: What if I what to have another controller inside my Area?

    Answer:

    Just add another controller beside AdminController and make sure the routes are like the following:

    [Area("Admin")]
    [Route("admin/[controller]")]
    public class ProductsController : Controller
    {
        public ProductsController()
        {
            //
        }
    
        [Route("{page:int?}")]
        public IActionResult Index()
        {
            return View();
        }
    }
    

    The important part is [Route("admin/[controller]")]. With that you can keep the style of routing to admin/controller/action/...

    0 讨论(0)
  • 2020-11-29 00:15

    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 :-)

    0 讨论(0)
提交回复
热议问题