2015-07-10 82 views
0

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

而且我很好奇,如果有在招搖UI支持在某種程度上顯示查詢參數。

本質上,我希望所有使用[EnableQueryAttribute]屬性標記的調用都有用於輸入查詢參數的swagger ui,並且我不想將這些參數添加到方法調用中,我仍然希望它們位於URL中並拔出歐文環境。

有什麼建議嗎?

回答

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());