Route Constraints

Route constraints let you restrict how the parameters in the route template are matched. The general syntax is "{parameter:constraint}". For example:

[Route("users/{id:int}"]
public User GetUserById(int id) { ... } [Route("users/{name}"]
public User GetUserByName(string name) { ... }

Here, the first route will only be selected if the "id" segment of the URI is an integer. Otherwise, the second route will be chosen.

The following table lists the constraints that are supported.

Constraint Description Example
alpha Matches uppercase or lowercase Latin alphabet characters (a-z, A-Z) {x:alpha}
bool Matches a Boolean value. {x:bool}
datetime Matches a DateTime value. {x:datetime}
decimal Matches a decimal value. {x:decimal}
double Matches a 64-bit floating-point value. {x:double}
float Matches a 32-bit floating-point value. {x:float}
guid Matches a GUID value. {x:guid}
int Matches a 32-bit integer value. {x:int}
length Matches a string with the specified length or within a specified range of lengths. {x:length(6)}
{x:length(1,20)}
long Matches a 64-bit integer value. {x:long}
max Matches an integer with a maximum value. {x:max(10)}
maxlength Matches a string with a maximum length. {x:maxlength(10)}
min Matches an integer with a minimum value. {x:min(10)}
minlength Matches a string with a minimum length. {x:minlength(10)}
range Matches an integer within a range of values. {x:range(10,50)}
regex Matches a regular expression. {x:regex(^\d{3}-\d{3}-\d{4}$)}

Notice that some of the constraints, such as "min", take arguments in parentheses. You can apply multiple constraints to a parameter, separated by a colon.

[Route("users/{id:int:min(1)}")]
public User GetUserById(int id) { ... }

最新文章

  1. 缺少.lib文件导致的Link2019 解决方案汇总
  2. C#-WinForm-发送邮件
  3. PHP5中使用PDO连接数据库的方法
  4. 鸡和蛋的OO设计
  5. 利用Shell命令获取IP地址
  6. JSP Workshop
  7. Spring EL regular expression example
  8. 手动添加删除windows服务
  9. jquery各版本区别
  10. 【读书笔记】Linux源码注释
  11. 洛谷 [P1198] 最大数
  12. 【Android】Mac下Android Studio设置App启动页
  13. 教你如何写出高效整洁的 css 代码——css优化(转载)
  14. Linux学习8-CentOS部署自己本地的django项目
  15. Restful framework【第六篇】认证组件
  16. spring applicationContext.xml
  17. HDFS shell操作及HDFS Java API编程
  18. NOIP模板总结
  19. Linux下设置oracle环境变量
  20. 【WPF】绑定Hyperlink超链接

热门文章

  1. Chapter 21_4 捕获
  2. vlc-android1.8.0的全部源代码[包括C语言]
  3. 了解OutOfMemoryError异常 - 深入Java虚拟机读后总结
  4. JavaScriptSerializer返回一条Json,页面获取值问题,数据绑定
  5. cursor属性
  6. Egret 学习之 入口函数 及开始编写程序(三)
  7. stylus or less ?
  8. unity3d和php后台简单交互--一
  9. Masonry的一些等间距布局
  10. 第一个Android crackme(2016-05)