官术网_书友最值得收藏!

Routing to areas

MVC has supported the concept of areas for a long time. Essentially, areas are for segregating and organizing controllers and views, so that, for example, you can have identically named controllers in different areas.

Visual Studio lets you create folders in a project and then add controllers and views to them. You can mark these folders as areas.

Where routing is concerned, areas add another route token, appropriately named area, to controller and action. If you are to use areas, you will likely have another segment in your template, such as this:

Products/Phones/Index
Reporting/Sales/Index

Here, Products and Reporting are areas. You need to map them to routes so that they are recognized by MVC. You can use the MapControllerRoute extension method, but you will need to supply the area token as follows:

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

You can also use the MapAreaControllerRoute extension method, which takes care of adding the area parameter:

endpoints.MapAreaControllerRoute(
name: "default",
areaName: "Products",
pattern: "List/{controller}/{action}/{id?}",
defaults: new { controller = "Phones", action = "Index" });

This route will map a request of List/Phones/Index to an Indexaction method of a PhonesControllercontroller inside the Products area.

That's it for areas. Let's now have a look at routing attributes.

主站蜘蛛池模板: 荥经县| 林甸县| 华池县| 林州市| 海原县| 绥中县| 安顺市| 伊宁县| 柳林县| 临安市| 大兴区| 广德县| 临高县| 改则县| 洛浦县| 广昌县| 苏尼特左旗| 三穗县| 青铜峡市| 凤山市| 黔西县| 上饶县| 金塔县| 宁河县| 阜平县| 东方市| 远安县| 栾川县| 溧阳市| 泽库县| 光泽县| 富川| 丰顺县| 手游| 广安市| 泰来县| 静海县| 格尔木市| 娄底市| 崇信县| 新安县|