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

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:

主站蜘蛛池模板: 新宾| 金门县| 沁阳市| 陕西省| 宁城县| 林西县| 海淀区| 九龙城区| 清水县| 吉隆县| 舟曲县| 浮山县| 金乡县| 正宁县| 龙陵县| 开远市| 会同县| 东台市| 沿河| 江门市| 鄄城县| 花垣县| 曲阜市| 绍兴县| 墨江| 县级市| 天全县| 攀枝花市| 马公市| 岗巴县| 靖远县| 县级市| 长春市| 宾川县| 罗平县| 邳州市| 赤水市| 乌什县| 潢川县| 任丘市| 灌阳县|