2015-02-08 89 views
1

我想重建我的上一個項目。在過去,我沒有使用任何Web API。 我可以使用ODataQueryOptions爲我的handler.ashx中的查詢執行$ filter,$ orderby,$ top,$ skip ? 有些事情。如何手動在.ashx上創建ODataQueryOptions

var option = new ODataQueryOptions(request.params); 
var query = option.ApplyTo(db.products); 
+0

是的,你可以看到[點擊這裏] (https://msdn.microsoft.com/en-us/library/system.web.http.odata.query.odataqueryoptions(v=vs.118).aspx) – qujck 2015-02-09 20:13:20

回答

0

我認爲你可以,如果你可以構造一個ODataQueryOptions的實例。

但是,這是什麼:

var option = new ODataQueryOptions(request.params); 

的Web API並沒有提供這樣的構造函數。它是你自己的實現嗎?

謝謝。

1

完成此操作的一種方法是手動構建請求URI並將其設置爲ODataQueryOptions構造函數的請求參數。所以這可能不是原始的海報正在尋找的東西(問題需要一些澄清)。

在我的情況下,我有一個單元測試,我想驗證odata選項被應用到我的可查詢對象。在下面的示例代碼中,假設您正在測試ProductController,其中包含ProductName字段。基於sfuqua的回答

// Manually set an OData query parameter 
const string restUrl = "http://localhost/api/product?$orderby=ProductName"; 

// Need to construct an HTTP Context and a Request, then inject them into the controller 
var config = new HttpConfiguration(); 
var request = new HttpRequestMessage(HttpMethod.Post, restUrl); 
var route = config.Routes.MapHttpRoute(WebApiConfig.DefaultRouteName, "api/{controller}/{id}"); 
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "Product" } }); 

var controller = new ProductController() 
{ 
    Request = request, 
    ControllerContext = new HttpControllerContext(config, routeData, request), 
    Url = new UrlHelper(request) 
}; 

// Build up the OData query parameters 
var modelBuilder = new ODataConventionModelBuilder(); 
modelBuilder.AddEntity(typeof(Product)); 
var edmModel = modelBuilder.GetEdmModel(); 
var oDataQueryContext = new ODataQueryContext(edmModel, typeof(Product)); 
var oDataQueryOptions = new ODataQueryOptions<Product>(oDataQueryContext, _controller.Request); 

// Finally, call the controller 
var result = controller.Get(oDataQueryOptions); 
0

上面我做我自己的助手基於OData的烏里OdataQueryOptions類,它的構造:

using System.Linq; 
using System.Net.Http; 
using System.Web.Http.OData; 
using System.Web.Http.OData.Builder; 
using System.Web.Http.OData.Query; 

namespace OdataHelpers 
{ 
    public static class ODataBuilder<T> 
    { 
     public static ODataQueryOptions<T> BuildOptions(string oDataUri) 
     { 
      var baseUri = ""; 
      var odUri = ""; 
      var spl = oDataUri.Split('?'); 
      if (spl.Count() == 0) 
       odUri = spl[0]; 
      else 
      { 
       baseUri = spl[0]; 
       odUri = spl[1]; 
      } 
      if (string.IsNullOrEmpty(baseUri)) 
       baseUri = "http://localhost/api/" + typeof(T).Name; 
      var request = new HttpRequestMessage(HttpMethod.Get, baseUri + "?" + oDataUri.Replace("?", "")); 
      var modelBuilder = new ODataConventionModelBuilder(); 
      modelBuilder.AddEntity(typeof(T)); 
      var edmModel = modelBuilder.GetEdmModel(); 
      var oDataQueryContext = new ODataQueryContext(edmModel, typeof(T)); 
      return new ODataQueryOptions<T>(oDataQueryContext, request); 
     } 
    } 
} 

使用例:

var OdataStuff = ODataBuilder<CustomerIntView>.BuildOptions("$orderby=Id"); 
+0

我已將此實現添加到我的ReverseOdataUriParser庫: https://開頭github上。 COM/Svakinn/ReverseOdataUriMapper – Svakinn 2017-12-29 18:44:45