使用FromUri绑定没有参数名称前缀的对象

Pau*_* K. 2 .net c# swagger asp.net-web-api2 swashbuckle

当使用ASP.NET WebApi2和Swashbuckle / Swagger时,我试图使用FromUri属性绑定对象,如下所示:

[RoutePrefix("api/v1/example")]
public class ExampleController : ApiController
{
    [HttpGet]
    [Route("{id}")]
    public ExampleModel Get(string id, [FromUri]ExampleModel searchCriteria)
    {
    ...
    }
}

public class ExampleModel
{
    public string id { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我发现执行此GET操作并按姓氏搜索的URL最终类似于:

http://localhost:52259/api/v1/example/1?searchCriteria.firstName=paul
Run Code Online (Sandbox Code Playgroud)

如何从查询参数中删除“ searchCriteria”前缀?我想将其保留为控制器方法签名中的ExampleModel对象,但可以使用(并将其反映在Swagger UI文档中):

http://localhost:52259/api/v1/example/1?firstName=paul
Run Code Online (Sandbox Code Playgroud)

Pau*_* K. 5

FromUri属性上有一个Name属性。如果将其设置为空字符串,则会使Swagger UI文档出现而没有“ searchCriteria”。字首:

public ExampleModel Get(string id, [FromUri(Name = "")]ExampleModel searchCriteria)
{
...
}
Run Code Online (Sandbox Code Playgroud)

似乎无论是否存在“ searchCriteria”,控制器都会接收名字。前缀作为查询参数名称的一部分。