- Hands-On Full:Stack Web Development with ASP.NET Core
- Tamir Dresher Amir Zuker Shay Friedman
- 144字
- 2021-06-10 19:37:30
Default values
Default values are defined by placing an equals sign next to the route parameter. Note that placing default values on the method parameters (not in the root template) will not work, as the routing pipeline is unable to find a match by looking at optional parameters.
For example, the GiveNTake application allows the user to search for products by specifying a category and a sub-category; however, the sub-category is optional, and if it is omitted, the default subcategory will be all. The following code snippet shows you how to define these rules:
[HttpGet("searchcategory/{category}/{subcategory=all}/")]
public string[] SearchByProducts(string category,string subcategory)
{
return new[]
{
$"Category: {category}, Subcategory: {subcategory}"
};
}
Run the application and navigate to http://localhost:[port]/api/products/searchcategory/furniture/kitchen, and then to http://localhost:[port]/api/products/searchcategory/furniture.
For the first URL, you should see results similar to the following:

And the second URL should produce an output like this:

- 微商之道
- 面向物聯(lián)網(wǎng)的CC2530與傳感器應(yīng)用開發(fā)
- Building Django 2.0 Web Applications
- Truffle Quick Start Guide
- 無人機通信
- 正在爆發(fā)的互聯(lián)網(wǎng)革命
- 計算機網(wǎng)絡(luò)工程實用教程(第2版)
- 城域網(wǎng)與廣域網(wǎng)(第2版)
- 物聯(lián)網(wǎng)工程導(dǎo)論(第3版)
- Laravel Application Development Cookbook
- 區(qū)塊鏈技術(shù)與應(yīng)用:打造分布式商業(yè)新生態(tài)
- 智能物聯(lián)安防視頻技術(shù)基礎(chǔ)與應(yīng)用
- 走近2050:注意力、互聯(lián)網(wǎng)與人工智能
- 萬物互聯(lián):物聯(lián)網(wǎng)核心技術(shù)與安全
- Hands-On Microservices:Monitoring and Testing