小编Pau*_* K.的帖子

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

当使用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)

.net c# swagger asp.net-web-api2 swashbuckle

2
推荐指数
1
解决办法
958
查看次数

标签 统计

.net ×1

asp.net-web-api2 ×1

c# ×1

swagger ×1

swashbuckle ×1