- 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:

- MERN Quick Start Guide
- 物聯(lián)網(wǎng)安全(原書第2版)
- 物聯(lián)網(wǎng)安全:理論、實(shí)踐與創(chuàng)新
- 社交電商運(yùn)營策略、技巧與實(shí)操
- 信息通信網(wǎng)絡(luò)建設(shè)安全管理概要2
- 面向云平臺的物聯(lián)網(wǎng)多源異構(gòu)信息融合方法
- NB-IoT物聯(lián)網(wǎng)技術(shù)解析與案例詳解
- Learning Swift(Second Edition)
- 網(wǎng)管員必讀:網(wǎng)絡(luò)管理(第2版)
- 6G新技術(shù) 新網(wǎng)絡(luò) 新通信
- Working with Legacy Systems
- SAE原理與網(wǎng)絡(luò)規(guī)劃
- 工業(yè)互聯(lián)網(wǎng)創(chuàng)新實(shí)踐
- React Cookbook
- 互聯(lián)網(wǎng)戰(zhàn)略變革與未來