在招摇UI的OData查询(OData query in swagger ui)

2019-10-23 19:22发布

我被检查出下面的教程: http://blogs.msdn.com/b/martinkearn/archive/2015/03/10/using-odata-query-syntax-with-web-api.aspx

我很好奇,如果有在招摇UI支持在某种程度上显示查询参数。

从本质上讲,我想标记[EnableQueryAttribute]属性来对输入查询参数招摇的用户界面,我不希望这些参数添加到方法调用我还是希望他们能在URL中掏出了Owin上下文的所有电话。

有什么建议么?

Answer 1:

答案是容易得多比我也是这么想的。 我落得这样做是创建一个IOperationFilter看着所有操作具有一定的回报类型和添加的参数吧。

class QueryParameterFilter : IOperationFilter
    {
        void IOperationFilter.Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            if (apiDescription.ResponseDescription.ResponseType != null && apiDescription.ResponseDescription.ResponseType.Name.Contains("PagedResult"))
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>()
                {
                    { "$top", "The max number of records"},
                    { "$skip", "The number of records to skip"},
                    { "$filter", "A function that must evaluate to true for a record to be returned"},
                    { "$select", "Specifies a subset of properties to return"},
                    { "$orderby", "Determines what values are used to order a collection of records"}
                };
                operation.parameters = new List<Parameter>();
                foreach (var pair in parameters)
                {
                    operation.parameters.Add(new Parameter
                    {
                        name = pair.Key,
                        required = false,
                        type = "string",
                        @in = "query",
                        description = pair.Value
                    });
                }
            }
        }

然后,他们可以通过owin上下文检索。

var params = owinContext.Request.Query.ToDictionary(p => p.Key, p => p.Value.FirstOrDefault());


文章来源: OData query in swagger ui