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

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.

主站蜘蛛池模板: 南江县| 宜宾县| 阿坝| 佛学| 留坝县| 余庆县| 吉安县| 西乌珠穆沁旗| 池州市| 彰化县| 富宁县| 浏阳市| 多伦县| 长宁县| 新河县| 博罗县| 苏尼特左旗| 金堂县| 深水埗区| 通城县| 贺兰县| 永德县| 阿克陶县| 卢湾区| 新安县| 南雄市| 文昌市| 古丈县| 浮山县| 政和县| 永德县| 航空| 时尚| 会同县| 云龙县| 潼南县| 湄潭县| 新竹县| 嘉黎县| 邮箱| 平江县|