- 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.
推薦閱讀
- 物聯(lián)網(wǎng)與北斗應(yīng)用
- Aptana Studio Beginner's Guide
- 光網(wǎng)絡(luò)評估及案例分析
- 物聯(lián)網(wǎng)+BIM:構(gòu)建數(shù)字孿生的未來
- 物聯(lián)網(wǎng)信息安全
- 世界互聯(lián)網(wǎng)發(fā)展報告·2019
- Getting Started with nopCommerce
- 云工廠:開啟中國制造云時代
- Enterprise ApplicationDevelopment with Ext JSand Spring
- 物聯(lián)網(wǎng),So Easy!
- RestKit for iOS
- Cisco無線局域網(wǎng)配置基礎(chǔ)
- Next.js Quick Start Guide
- 5G智聯(lián)萬物:輕松讀懂5G應(yīng)用與智能未來
- 網(wǎng)絡(luò)攻防技術(shù)與實踐