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

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:

主站蜘蛛池模板: 星座| 新化县| 遂平县| 达尔| 阿拉善盟| 清新县| 凤山县| 镇赉县| 福贡县| 雷州市| 恩施市| 利津县| 库尔勒市| 格尔木市| 丹寨县| 寿宁县| 茂名市| 浦东新区| 宁南县| 白山市| 民县| 贵南县| 淮安市| 博白县| 金溪县| 连江县| 张家界市| 孝昌县| 兴业县| 肇州县| 韩城市| 永兴县| 海盐县| 佛山市| 嘉定区| 织金县| 新巴尔虎右旗| 酒泉市| 巴林右旗| 若羌县| 宁蒗|