RC的WebAPI与GETALL的URI参数 [英] WebAPI RC GetAll with URI parameters
问题描述
previously中的WebAPI(测试版),我能够创造一个GETALL方法,它参加了在URI中添加了可选的参数:
Previously in WebAPI (beta) I was able to create a "GetAll" method that took in optional parameters added on the URI:
http://localhost/api/product?take=5&skip=10
这似乎仍然工作,但只有当我包括所有的参数。在(测试版),我可以忽略的参数(的http://本地主机/ API /产品/
)和GETALL的方法将被调用(以&安培;跳过会为null)。我也可以省略一些参数的http://本地主机/ API /产品取= 5
(跳过会为null)
This still seems to work but only if I include all the parameters. In (beta), I could omit the parameters ( http://localhost/api/product/
) and the "GetAll" method would get called (take & skip would be null). I could also omit some of the parameters http://localhost/api/product?take=5
(skip would be null)
public IEnumerable<ProductHeaderDto> GetAll(int? take, int? skip)
{
var results = from p in productRepository
select new ProductHeaderDto
{
Id = p.Id,
Version = p.Version,
Code = p.Code,
Description = p.DescriptionInternal,
DisplayName = p.Code + " " + p.DescriptionInternal
};
if (skip != null) results = results.Skip(skip.Value);
if (take != null) results = results.Take(take.Value);
return results;
}
在(RC),现在我得到没有采取任何的控制器产品与请求匹配找到。如果参数的两个或者一个人失踪。我曾尝试在方法的参数中加入[FromUri]但是这并没有影响:
In (RC), I now get "No action was found on the controller 'Product' that matches the request." when both or one of the parameters are missing. I have tried adding [FromUri] on the method parameters but that has no affect:
public IEnumerable<ProductHeaderDto> GetAll([FromUri] int? take, [FromUri] int? skip)
我自己也尝试设置默认值:
I have also tried setting default values:
public IEnumerable<ProductHeaderDto> GetAll(int? take = null, int? skip = null)
时有某种形式的可选的参数属性,可以试图匹配方法签名时,可以使用?
Is there some sort of "optional" parameter attribute that could be used when trying to match the method signature?
推荐答案
这是已被固定在RTM的错误。您可以通过指定默认值可选参数。
This is a bug which has been fixed in RTM. You can have optional parameter by specifying default value.
public IEnumerable<string> Get(int? take = null, int? skip = null)
顺便说一句,你可以用$跳过和$顶部网页API ODATA包< /一>来实现相同的功能。
BTW, you can use $skip and $top in web api odata package to achieve the same functions.
这篇关于RC的WebAPI与GETALL的URI参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!