我有以下 Web API (GET):
public class UsersController : ApiController
{
public IEnumerable<Users> Get(string firstName, string LastName, DateTime birthDate)
{
// Code
}
}
这是一个 GET,所以我可以这样称呼它:
http://localhost/api/users?firstName=john&LastName=smith&birthDate=1979/01/01
并接收用户的 xml 结果。
是否可以将参数封装到一个类中,如下所示:
public class MyApiParameters
{
public string FirstName {get; set;}
public string LastName {get; set;}
public DateTime BirthDate {get; set;}
}
然后有:
public IEnumerable<Users> Get(MyApiParameters parameters)
我已经尝试过,并且任何时候我尝试得到结果http://localhost/api/users?firstName=john&LastName=smith&birthDate=1979/01/01
, the parameter
一片空白。
默认情况下,复杂类型是从 body 中读取的,这就是为什么你得到 null 的原因。
将您的操作签名更改为
public IEnumerable<Users> Get([FromUri]MyApiParameters parameters)
如果您希望模型绑定器从查询字符串中提取模型。
您可以在 MSFT 的 Mike Stall 撰写的优秀文章中阅读有关 Web API 如何进行参数绑定的更多信息 -http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)