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

Constraints

Inline constraints inside the routing attributes are used by placing a colon with the constraint name :constraint-name  after the route parameter name, where constraint-name is a constraint that you define by creating a class that implements the IRouteConstraint interface, or simply by using one of the built-in constraints specified in the ASP.NET documentation, available at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?highlight=routing#route-constraint-reference.

The following example shows how we can add a search method to our ProductsController that searches by the date that the product was posted online. The URL for this action must constrain the date parameter to datetime formats only; therefore, we will use the datetime constraint like this:

[HttpGet("search/{date:datetime}/{keyword}/")]
public string[] Search(string date, string keyword)
{
return new[]
{
$"Date: {date}, keyword: {keyword}"
};
}

The routing infrastructure in ASP.NET Core is very sophisticated, and there are plenty more features out of the scope of this book that therefore aren't covered. For more details about the routing capabilities in ASP.NET Core, refer to the documentation at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing.

主站蜘蛛池模板: 明光市| 怀柔区| 新化县| 榆林市| 易门县| 大庆市| 河池市| 桃园市| 宿迁市| 伊川县| 哈巴河县| 郯城县| 榆树市| 山东| 普安县| 凤翔县| 彭水| 安龙县| 额济纳旗| 乐山市| 泗阳县| 鲁甸县| 噶尔县| 宣武区| 子长县| 桂东县| 论坛| 洛阳市| 临西县| 安阳县| 泸溪县| 石河子市| 洞头县| 色达县| 达日县| 晋州市| 慈溪市| 教育| 平湖市| 武城县| 兰西县|