如何在.ashx上手动创建ODataQueryOptions

时间:2015-02-08 12:25:08

标签: asp.net-web-api odata

我想重建我的上一个项目。 在过去,我没有使用任何Web API。 我可以使用ODataQueryOptions来做$ filter,$ orderby,$ top,$ skip 我在自己的handler.ashx中的查询? 有点像。

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

3 个答案:

答案 0 :(得分:2)

实现此目的的一种方法是手动构建请求URI并在ODataQueryOptions构造函数的request参数中设置该URI。所以这可能不是原始海报所寻求的(问题需要澄清)。

在我的情况下,我有一个单元测试,我想验证odata选项是否应用于我的可查询对象。在以下示例代码中,假设您正在测试其中包含ProductName字段的ProductController

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

答案 1 :(得分:0)

我认为你可以构建一个ODataQueryOptions的实例。

但是,这是什么:

var option = new ODataQueryOptions(request.params);

Web API不提供此类构造函数。这是你自己的实现吗?

感谢。

答案 2 :(得分:0)

基于sfuqua的上述答案,我创建了自己的助手类,基于Odata Uri构建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");