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

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:

主站蜘蛛池模板: 台州市| 名山县| 涞源县| 内江市| 湖口县| 商都县| 黔西| 晴隆县| 岳西县| 肥乡县| 永福县| 班戈县| 宁海县| 武胜县| 卢湾区| 年辖:市辖区| 榕江县| 精河县| 胶南市| 思茅市| 太湖县| 大丰市| 商丘市| 平原县| 潜山县| 于都县| 中宁县| 滦南县| 西丰县| 伊春市| 盱眙县| 历史| 屯留县| 财经| 潞西市| 丹巴县| 遂昌县| 黄大仙区| 花莲县| 鹤庆县| 汕尾市|