官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 邢台县| 合阳县| 轮台县| 壤塘县| 永寿县| 肃南| 安图县| 上栗县| 嘉祥县| 宜春市| 永修县| 黄石市| 永州市| 讷河市| 库尔勒市| 长寿区| 宝丰县| 连云港市| 北辰区| 积石山| 张家川| 宝兴县| 顺昌县| 得荣县| 潜江市| 伊春市| 石城县| 拉萨市| 天镇县| 邳州市| 望奎县| 寻乌县| 南丹县| 曲水县| 河西区| 甘谷县| 耿马| 邮箱| 乾安县| 伊金霍洛旗| 多伦县|