- Hands-On Full:Stack Web Development with ASP.NET Core
- Tamir Dresher Amir Zuker Shay Friedman
- 173字
- 2021-06-10 19:37:27
Adding routes via route attributes
Adding routes via route attributes is a way of defining the route for every controller action as an attribute on top of controller action methods.
Within the controller class (in our case, HomeController), add the Route attribute before the Index method:
[Route("Home/Index")]
public IActionResult Index()
{
return View();
}
This instructs ASP.NET Core MVC to pass requests to the Index method whenever the URL looks like http://example.com/Home/Index. As patterns are not supported in attribute routing, if we want to make the Index method of HomeController the default of the application, then we need to add multiple Route attribute calls on top of the Index method:
[Route("")]
[Route("Home")]
[Route("Home/Index")]
public IActionResult Index()
{
return View();
}
Attribute routing makes it easier to add new routes and keep them up-to-date with name changes or architectural code changes, but it doesn't support complicated patterns and makes it harder to see the full route list of the application. Attribute routing is, in most cases, the preferred way for ASP.NET Core web APIs.
- 廣電5G從入門到精通
- 面向物聯網的CC2530與傳感器應用開發
- C++黑客編程揭秘與防范
- MERN Quick Start Guide
- 物聯網關鍵技術及應用
- 計算機網絡與通信(第2版)
- NB-IoT物聯網技術解析與案例詳解
- 電力物聯網工程技術原理與應用
- jQuery Mobile Web Development Essentials
- Learning Windows 8 Game Development
- Guide to NoSQL with Azure Cosmos DB
- 移動互聯網新思維
- 物聯網M2M開發技術:基于無線CPU-Q26XX
- React Design Patterns and Best Practices(Second Edition)
- 智能物聯網:區塊鏈與霧計算融合應用詳解