- 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.
- Modern JavaScript Web Development Cookbook
- 光網絡評估及案例分析
- 物聯網網絡安全及應用
- 5G承載網網絡規劃與組網設計
- 網絡故障現場處理實踐(第4版)
- 物聯網關鍵技術及應用
- HCNA網絡技術
- 網絡互聯技術(實踐篇)
- Building RESTful Web Services with Spring 5(Second Edition)
- Getting Started with Grunt:The JavaScript Task Runner
- Learning Swift(Second Edition)
- 物聯網之霧:基于霧計算的智能硬件快速反應與安全控制
- 網絡環境中基于用戶視角的信息質量評價研究
- Working with Legacy Systems
- Learning Storm