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/1033 and /api/books/locale return 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.)

webapi中Route标签定义可选参数的更多相关文章

  1. 在webapi中为Action使用dynamic参数实现Post方式调用

    1.在webapi中使用controller/action/id的路径配置,打开文件[App_Start] -[WebApiConfig] config.Routes.MapHttpRoute( na ...

  2. 通过maven中properties标签定义spring版本号

    一 发现问题 在pom.xml中添加依赖时语法如下 <dependency>  <groupId>org.springframework</groupId>  &l ...

  3. maven中properties标签定义变量

    在pom.xml中添加依赖时语法如下 <dependency> <groupId>org.springframework</groupId> <artifac ...

  4. web api Route属性定义

    ASP.NET Web API路由,简单来说,就是把客户端请求映射到对应的Action上的过程.在"ASP.NET Web API实践系列03,路由模版, 路由惯例, 路由设置"一 ...

  5. Asp.Net Core WebAPI入门整理(四)参数获取

    一.总结整理,本实例对应.Net Core 2.0版本 1.在.Net Core WebAPI 中对于参数的获取及自动赋值,沿用了Asp.Net  MVC的有点,既可以单个指定多个参数,右可以指定Mo ...

  6. mybatis中<include>标签的作用

    MyBatis中sql标签定义SQL片段,include标签引用,可以复用SQL片段 sql标签中id属性对应include标签中的refid属性.通过include标签将sql片段和原sql片段进行 ...

  7. [读书笔记]C#学习笔记七: C#4.0中微小改动-可选参数,泛型的可变性

    前言 下面就开始总结C#4.0的一些变化了, 也是这本书中最后的一点内容了, 这一部分终于要更新完了. 同时感觉再来读第二遍也有不一样的收获. 今天很嗨的是武汉下雪了,明天周六,一切都是这么美好.哈哈 ...

  8. C#中的 具名参数 和 可选参数

    具名参数 和 可选参数 是 C# framework 4.0 出来的新特性. 一. 常规方法定义及调用 public void Demo1(string x, int y) { //do someth ...

  9. PHP中的可变参数函数和可选参数函数

    1)可选参数函数.例如: <?phpfunction add($var1,$var2,$var3=0,$var4=0){ return$var1+$var2+$var3+$var4;}echo ...

随机推荐

  1. C#程序基础

  2. jquery验证表单是否满足正则表达式是否通过验证例子

    //验证通用函数 a表示元素对象,b表示正则表达式,c存bool值 function testyz(a,b,c){ c=false; $(a).on("blur",function ...

  3. HTTP Response Splitting攻击探究 <转>

    第一小节:HTTP Basics:使用Proxy软件(例如Webscarab)来截断浏览器(客户端)和Server之间的HTTP通信,之后任意篡改得到预期结果即可. 第二小节:HTTP Splitti ...

  4. HDU 5904 LCIS

    $dp$. 这题的突破口在于要求数字是连续的. 可以分别记录两个串以某个数字为结尾的最长上升长度,然后枚举一下以哪个数字为结尾就可以得到答案了. 因为$case$有点多,不能每次$memset$,额外 ...

  5. jdk之String

    学习几个常用的String方法 1.concat /** * Concatenates the specified string to the end of this string. 连接指定的字符串 ...

  6. jmeter对http协议中post请求接口测试

     现在有很多的工具用于工作上的使用,在jmeter的开源工具当中的,提供了一个可以对http协议的post的请求上接口测试,用于实现接口测试的自动化测试,当然也可以使用自己写的工具. 进行打开jmet ...

  7. 给go添加各种package

    go version 1.1.2 For example you need to install the webscoket pakeage try           go get code.goo ...

  8. 华硕笔记本进pe之前的设置

    1.开机的时候长按F2键进入BIOS界面,通过方向键进[Secure]菜单,通过方向键选择[Secure Boot Control]选项,将其设定为 "Disabled"2.通过方 ...

  9. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

  10. JavaFX基础学习之OkHttp/Gson2

    222 package application; import java.io.IOException; import java.net.URL; import java.util.ResourceB ...