ODataQueryOptions始终为null

时间:2016-08-31 09:56:05

标签: c# asp.net asp.net-web-api2 odata devextreme

这是我第一次使用ODataQueryOptions并且不知道制作一个asp.net web api 2方法GET使用这个,我在我的项目中有这个代码:

WebApiConfig

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ...
        config.AddODataQueryFilter();
        ...
    }
}

ApiController

[Authorize]
[RoutePrefix("api/dtoobjects")]
public class DtoObjectsController : ApiController
{
    private readonly DtoObjectManager m_dtoObjectManager = new DtoObjectManager();

    [EnableQuery]
    public IQueryable<DtoObject> Get(ODataQueryOptions<DtoObject> options)
    {
        var dtoObjects = m_dtoObjectManager.Get(...);

        IQueryable results = options.ApplyTo(dtoObjects.AsQueryable());

        return results as IQueryable<DtoObject>;
    }
}

AjaxRequest(DevExtreme自定义商店)

var store = new DevExpress.data.CustomStore({
    load: function(loadOptions) {
        var d = $.Deferred();

        var params = {};

        //Getting filter options
        if (loadOptions.filter) {
            params.filter = JSON.stringify(loadOptions.filter);
        }

        //Getting sort options
        if (loadOptions.sort) {
            params.sort = JSON.stringify(loadOptions.sort);
        }

        //Getting group options
        if (loadOptions.group) {
            params.group = JSON.stringify(loadOptions.group);
        }

        //skip and take are used for paging
        params.skip = loadOptions.skip; //A number of records that should be skipped
        params.take = loadOptions.take; //A number of records that should be taken

        //If the select expression is specified
        if (loadOptions.select) {
            params.select = JSON.stringify(loadOptions.select);
        }

        //If a user typed something in dxAutocomplete, dxSelectBox or dxLookup
        if (loadOptions.searchValue) {
            params.searchValue = loadOptions.searchValue;
            params.searchOperation = loadOptions.searchOperation;
            params.searchExpr = loadOptions.searchExpr;
        }

        $.ajax({
            type: "GET",
            url: "/api/dtoobjects",
            contentType: "application/json; charset=utf-8",
            data: params,
            dataType: "json",
            success: function (response) {
                d.resolve(response, { totalCount: response.length });
            }
        });
        return d.promise();
    } 

当ajax请求触发GET方法时,“ODataQueryOptions选项”总是为空!

enter image description here

我做错了什么?

感谢您的关注!

0 个答案:

没有答案