使用Route属性时模型为null [英] Model null when use Route attribute
本文介绍了使用Route属性时模型为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的 WebAPI
中,我有模型
public class ListRequest
{
public int Skip { get; set; } = 0;
public int Take { get; set; } = 30;
}
我的动作是
[HttpGet]
[Route("api/users")]
public IHttpActionResult Get([FromUri] ListRequest request) {
...
}
我需要不传递任何查询参数,然后应使用默认值.但是,当我转到 http://localhost:44514/api/users
时, request
为null.如果删除 [Route("api/users")]
,则 request
不为null,并且具有参数的默认值.
I need to have possibility to not pass any query parameters, then default values should be used. But, when I go to http://localhost:44514/api/users
the request
is null. If I remove [Route("api/users")]
then request
is not null and has default values for parameters.
如何通过Route属性实现该行为?
How can I reach that behavior with Route attribute?
推荐答案
如果要使用Route属性初始化模型,请尝试
If you want to init model using Route attributes try
Route("api/users/{*pathvalue}")]
这篇关于使用Route属性时模型为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文