这是我第一次使用ODataQueryOptions并且不知道制作一个asp.net web api 2方法GET使用这个,我在我的项目中有这个代码:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
...
config.AddODataQueryFilter();
...
}
}
[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>;
}
}
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选项”总是为空!
我做错了什么?
感谢您的关注!