- 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.
- Learn ECMAScript(Second Edition)
- 從零開始構建企業級RAG系統
- JavaScript百煉成仙
- Microsoft Dynamics 365 Extensions Cookbook
- C#程序設計教程
- Visual C++應用開發
- TradeStation交易應用實踐:量化方法構建贏家策略(原書第2版)
- JavaScript 程序設計案例教程
- Advanced Oracle PL/SQL Developer's Guide(Second Edition)
- Web Development with MongoDB and Node(Third Edition)
- 零基礎趣學C語言
- 劍指大數據:企業級數據倉庫項目實戰(在線教育版)
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- Python:Deeper Insights into Machine Learning
- C語言從入門到精通