webapi中Route标签定义可选参数
2016-01-04 17:01
537 查看
Optional URI Parameters and Default Values
You can make a URI parameter optional by adding a question mark to the route parameter. If a route parameter is optional, you must define a default value for the method parameter.public class BooksController : ApiController { [Route("api/books/locale/{lcid:int?}")] public IEnumerable<Book> GetBooksByLocale(int lcid = 1033) { ... } }
In this example,
/api/books/locale/1033and
/api/books/localereturn the same resource.
Alternatively, you can specify a default value inside the route template, as follows:
public class BooksController : ApiController { [Route("api/books/locale/{lcid:int=1033}")] public IEnumerable<Book> GetBooksByLocale(int lcid) { ... } }
This is almost the same as the previous example, but there is a slight difference of behavior when the default value is applied.
In the first example ("{lcid?}"), the default value of 1033 is assigned directly to the method parameter, so the parameter will have this exact value.
In the second example ("{lcid=1033}"), the default value of "1033" goes through the model-binding process. The default model-binder will convert "1033" to the numeric value 1033. However, you could plug in a custom model binder, which might do something different.
(In most cases, unless you have custom model binders in your pipeline, the two forms will be equivalent.)
相关文章推荐
- Cron 表达式详解和案例
- GIS习题:制作一个上海地铁(进一步考虑到公交)的最佳点到点路径规划生成算法?
- 网络安全架构师必修课)生产环境下的 wireshark 数据包捕获,过滤,分析技巧与实战
- LOG4NET 使用方法
- libevent 读书笔记
- 【题解】QDUOJ.65.again and again
- 使用WindowManager.LayoutParams的x和y设置窗体位置
- 一些java资源
- grep正则表达式-高级
- 一位IT程序员的心声:我们是无耻之徒吗?
- NotePad++ 列模式(在多行开头统一添加相同内容)
- Linux下gsoap实现webservice功能
- Python中的__name__和__main__含义详解
- TreeView递归绑定数据的两种方法
- 蓝桥杯-错误票据 (水)
- Js 中关于注册表单,使用onsubmit进行拦截提交,并判断用户填写的内容是否正确
- jmeter学习 二
- jQuery EasyUI之DataGrid使用实例详解
- mysql备份和还原(1)
- 数组,字符串