- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 218字
- 2021-06-18 18:35:59
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.
- HTML5+CSS3王者歸來(lái)
- Python 深度學(xué)習(xí)
- Oracle從入門到精通(第5版)
- 深入理解Elasticsearch(原書第3版)
- INSTANT Sinatra Starter
- 現(xiàn)代C++編程實(shí)戰(zhàn):132個(gè)核心技巧示例(原書第2版)
- Python 3 數(shù)據(jù)分析與機(jī)器學(xué)習(xí)實(shí)戰(zhàn)
- Python Programming for Arduino
- Spring Boot 2+Thymeleaf企業(yè)應(yīng)用實(shí)戰(zhàn)
- Scala編程(第4版)
- Internet of Things with Arduino Cookbook
- Python程序員面試算法寶典
- Python AI游戲編程入門:基于Pygame和PyTorch
- 匯編語(yǔ)言程序設(shè)計(jì)
- C++教程