swagger UI中的OData查询 [英] OData query in swagger ui
问题描述
I was checking out the following tutorial: http://blogs.msdn.com/b/martinkearn/archive/2015/03/10/using-odata-query-syntax-with-web-api.aspx
我很好奇,是否以某种方式在swagger ui中提供了支持以显示查询参数.
And I was curious if there was support in swagger ui somehow to show the query parameters.
基本上,我希望所有带有[EnableQueryAttribute]属性标记的调用都具有用于输入查询参数的大写ui,并且我不想将这些参数添加到方法调用中,但我仍然希望它们位于URL中并拉出以用于奥文语境.
Essentially I wanted all calls tagged with [EnableQueryAttribute] attribute to have swagger ui for inputting query parameters and I don't want to add these parameters to the method call I still want them to be in the URL and pulled out for the Owin context.
有什么建议吗?
推荐答案
答案比我想象的要容易得多.我最终要做的是创建一个IOperationFilter,并查找具有特定返回类型的所有操作,然后向其中添加参数.
The answer was much easier than I was thinking. What I ended up doing is creating an IOperationFilter and looked for all Operations with a certain return type and added the Parameters to it.
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上下文检索它们.
And then they can retrieved through the owin context.
var params = owinContext.Request.Query.ToDictionary(p => p.Key, p => p.Value.FirstOrDefault());
这篇关于swagger UI中的OData查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!