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

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.

主站蜘蛛池模板: 西青区| 墨竹工卡县| 乌审旗| 泽州县| 克什克腾旗| 六枝特区| 海林市| 古浪县| 龙山县| 江孜县| 拜泉县| 永城市| 南阳市| 宝坻区| 夏邑县| 余姚市| 通海县| 朝阳市| 兴安盟| 云浮市| 玛纳斯县| 昭平县| 墨江| 崇义县| 禹城市| 普宁市| 大姚县| 彩票| 万年县| 峡江县| 大兴区| 同江市| 富阳市| 同德县| 铁岭市| 宜昌市| 富锦市| 巴里| 武义县| 泸溪县| 壤塘县|