带有URI参数的WebAPI RC GetAll



以前在WebAPI(测试版)中,我能够创建一个"GetAll"方法,该方法接受添加在URI上的可选参数:

http://localhost/api/product?take=5&skip=10 

这似乎仍然有效,但前提是我包含了所有参数。在(beta)中,我可以省略参数(http://localhost/api/product/),并调用"GetAll"方法(take&skip将为null)。我也可以省略一些参数http://localhost/api/product?take=5(跳过将为空)

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)中,当两个或其中一个参数都丢失时,我现在得到"在与请求匹配的控制器'Product'上找不到任何操作"。我已经尝试在方法参数上添加[FromUri],但这没有影响:

public IEnumerable<ProductHeaderDto> GetAll([FromUri] int? take, [FromUri] int? skip)

我也尝试过设置默认值:

public IEnumerable<ProductHeaderDto> GetAll(int? take = null, int? skip = null)

在尝试匹配方法签名时,是否可以使用某种"可选"参数属性?

这是RTM中修复的一个错误。您可以通过指定默认值来设置可选参数。

public IEnumerable<string> Get(int? take = null, int? skip = null)

顺便说一句,你可以在web api odata包中使用$skip和$top来实现相同的功能。

最新更新