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

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.

主站蜘蛛池模板: 三亚市| 睢宁县| 沂南县| 应用必备| 铅山县| 沭阳县| 金坛市| 庆城县| 柘荣县| 环江| 广河县| 云浮市| 林甸县| 革吉县| 上虞市| 长白| 新田县| 宝坻区| 元阳县| 阜南县| 金沙县| 泾川县| 达拉特旗| 江达县| 繁峙县| 通许县| 时尚| 锡林郭勒盟| 乌鲁木齐市| 安仁县| 淳化县| 阿克苏市| 牡丹江市| 拜泉县| 岱山县| 漳浦县| 黎川县| 河源市| 稷山县| 健康| 乌兰县|