- Hands-On Full:Stack Web Development with ASP.NET Core
- Tamir Dresher Amir Zuker Shay Friedman
- 196字
- 2021-06-10 19:37:29
Parameterizing the route template
Attribute-based routing supports a few predefined tokens that are placed in square brackets ([ and ]), and will be replaced at runtime with their corresponding value:
- [controller]: This will be replaced with the controller name.
- [action]: This will be replaced with the method name.
- [area]: If your application supports areas, this will be replaced with the area in which the controller resides. Area functionality is not covered in this book, but for more information, you can refer to https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas.
For example, instead of writing ProductsController explicitly in the RouteAttribute, we can write it like this:
[Route("api/[controller]")]
[ApiController]
public class ProductsController : Controller
{
...
}
Tokens within curly braces ({}) define route parameters that will be bound to the method parameters if the route is matched.
For example, suppose you wish to expose an API to search for products, based on a keyword, in the form of a GET request to a URL formatted as /api/products/search/keyword. This is how you can write it:
[HttpGet("search/{keyword}")]
public string[] SearchProducts(string keyword)
{
...
}
Just like with conventional routing, you can define default values and constraints on the route parameters.
推薦閱讀
- RCNP實驗指南:構建高級的路由互聯網絡(BARI)
- 物聯網工程規劃技術
- 物聯網識別技術
- 工業控制網絡安全技術與實踐
- 信息通信網絡建設安全管理概要2
- Windows Server 2003 Active Directory Design and Implementation: Creating, Migrating, and Merging Networks
- 互聯網安全的40個智慧洞見:2014年中國互聯網安全大會文集
- 物聯網技術與應用
- IPv6網絡切片:使能千行百業新體驗
- Echo Quick Start Guide
- Scala Design Patterns.
- Qt5 Python GUI Programming Cookbook
- Intelligent Mobile Projects with TensorFlow
- 數字王國里的虛擬人:技術、商業與法律解讀
- Python API Development Fundamentals