- 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.
- Mastering Node.js(Second Edition)
- 數字烏托邦
- Hands-On Full Stack Development with Spring Boot 2 and React(Second Edition)
- 局域網組建、管理與維護項目教程(Windows Server 2003)
- 面向物聯網的嵌入式系統開發:基于CC2530和STM32微處理器
- Building RESTful Web Services with Spring 5(Second Edition)
- PLC、現場總線及工業網絡實用技術速成
- Socket.IO Real-time Web Application Development
- 計算機網絡與通信(第2版)
- SSL VPN : Understanding, evaluating and planning secure, web/based remote access
- WordPress Web Application Development
- 網絡安全應急響應技術實戰指南
- 網絡AI+:2030后的未來網絡
- OSPF協議原理與功能拓展
- ElasticSearch Server