- 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.
推薦閱讀
- 計算機網絡與通信(第2版)
- Mastering Machine Learning for Penetration Testing
- 異構基因共表達網絡的分析方法
- Hands-On Industrial Internet of Things
- Proxmox High Availability
- 智慧城市中的移動互聯網技術
- 數字通信同步技術的MATLAB與FPGA實現:Altera/Verilog版(第2版)
- 5G技術與標準
- Master Apache JMeter:From Load Testing to DevOps
- SAE原理與網絡規劃
- 網絡AI+:2030后的未來網絡
- Getting Started with nopCommerce
- 5G非正交多址接入技術:理論、算法與實現
- 物聯網M2M開發技術:基于無線CPU-Q26XX
- 物聯網商業設計與案例