Jer*_*eme 2 asp.net odata asp.net-web-api2 swashbuckle
我有一个支持 OData 查询的 ASP.Net Web API 2 端点。它是这样的:
[HttpGet, Route("")]
public IQueryable<Thing> Get()
{
return _thingsRepository.Query();
}
Run Code Online (Sandbox Code Playgroud)
OData $filter 参数等效果很好。我只是希望它们能像在实际的 OData 控制器中那样出现在 Swagger 中。
我正在使用 Swashbuckle.OData ......但我真的不确定在这种情况下它会给我买什么。
事实证明,使用 SwashBuckle 使用IOperationFilter. 您可以使用c.OperationFilter<ODataParametersSwaggerDefinition>();. 我创建了一个将一些 OData 参数添加到我的 API 中的所有 IQueryable 端点:
/// <summary>
/// Add the supported odata parameters for IQueryable endpoints.
/// </summary>
public class ODataParametersSwaggerDefinition : IOperationFilter
{
private static readonly Type QueryableType = typeof(IQueryable);
/// <summary>
/// Apply the filter to the operation.
/// </summary>
/// <param name="operation">The API operation to check.</param>
/// <param name="schemaRegistry">The swagger schema registry.</param>
/// <param name="apiDescription">The description of the api method.</param>
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var responseType = apiDescription.ResponseType();
if (responseType.GetInterfaces().Any(i => i == QueryableType))
{
operation.parameters.Add(new Parameter
{
name = "$filter",
description = "Filter the results using OData syntax.",
required = false,
type = "string",
@in = "query"
});
operation.parameters.Add(new Parameter
{
name = "$orderby",
description = "Order the results using OData syntax.",
required = false,
type = "string",
@in = "query"
});
operation.parameters.Add(new Parameter
{
name = "$skip",
description = "The number of results to skip.",
required = false,
type = "integer",
@in = "query"
});
operation.parameters.Add(new Parameter
{
name = "$top",
description = "The number of results to return.",
required = false,
type = "integer",
@in = "query"
});
operation.parameters.Add(new Parameter
{
name = "$count",
description = "Return the total count.",
required = false,
type = "boolean",
@in = "query"
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2172 次 |
| 最近记录: |