有没有办法让Swashbakle将OData参数添加到Web API2IQueryable<T>端点?

Is there a way to get Swashbuckle to add OData parameters to Web API 2 IQueryablelt;Tgt; endpoint?(有没有办法让Swashbakle将OData参数添加到Web API2IQueryablelt;Tgt;端点?)

本文介绍了有没有办法让Swashbakle将OData参数添加到Web API2IQueryable<T>端点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个支持OData查询的ASP.NET Web API 2终结点。是这样的:

[HttpGet, Route("")]
public IQueryable<Thing> Get()
{
    return _thingsRepository.Query();
}

odata$过滤参数等非常有用。我只希望它们能像实际的OData控制器一样出现在Swagger中。

我正在使用Swashbakle.OData.但我真的不确定在这种情况下它能给我带来什么。

推荐答案

事实证明,使用IOperationFilter使用swashbakle为swagger添加您想要的任何参数非常容易。您可以使用c.OperationFilter<ODataParametersSwaggerDefinition>();将其添加到Swagger配置中。我创建了一个将一些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"
            });
        }
    }
}

这篇关于有没有办法让Swashbakle将OData参数添加到Web API2IQueryable&lt;T&gt;端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:有没有办法让Swashbakle将OData参数添加到Web API2IQueryable&lt;T&gt;端点?

基础教程推荐