在基础ApiController中使用PageResult会返回System.ArgumentNullException

时间:2014-11-04 02:42:38

标签: .net linq odata asp.net-web-api2

我正在尝试编写一个已添加到基本控制器的通用PageResult

    [EnableQuery]
    [HttpGet]
    [Route(""]
    public virtual PageResult<TListModel> Get(ODataQueryOptions<TListModel> options)
    {
        ODataQuerySettings settings = new ODataQuerySettings()
        {
            PageSize = 25,
        };

        IQueryable results = options.ApplyTo(DomainService.GetQueryable(), settings);
        var items = results as IQueryable<TListModel>;
      // return new PageResult<TListModel>(items, Request.ODataProperties().NextLink,DomainService.GetQueryable().Count());
        return new PageResult<TListModel>(items, Request.ODataProperties().NextLink, Request.ODataProperties().TotalCount);

    }

现在我尝试调用api端点

/endpoint?$select=Title

我收到错误

  

值不能为空。\\参数名称:source

如果我删除?$select=Title我可以调用api并返回结果。

我是否应该采取其他措施来启用此功能?参数名称源....我不知道这是什么?

完整的堆栈跟踪

  

&#34;在System.Linq.Queryable.Count [TSource](IQueryable 1 source)\ \ at Api.EndPoints.BaseController 5.Get(ODataQueryOptions 1 options) in e:\\@Source\\Web\\Api\\.EndPoints\\BaseController.cs:line 62\ \ at lambda_method(Closure , Object , Object[] )\ \ at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\ \ at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\ \ at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary 2个参数,CancellationToken cancellationToken)\ \ ---从先前位置抛出异常的堆栈跟踪结束--- \\ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\ \ at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\ \ --- End of stack trace from previous location where exception was thrown ---\ \ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\ \ at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()\ \ ---从抛出异常的上一个位置开始的堆栈跟踪--- \\ at System.Runtime.ExceptionServices System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()的\ ExceptionDispatchInfo.Throw()\ \ \ --- ---从抛出异常的上一个位置的堆栈跟踪结束--- \\在System.Runtime。 CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ \在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Tas k任务)\ \ at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\ \ at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()\ \ --- End of stack trace from previous location where exception was thrown ---\ \ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\ \ at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\ \ ---堆栈跟踪结束从以前抛出异常的位置--- \ \在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ \在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ \在System.Runtime.CompilerServices .TaskAwaiter`1.GetResult()\ \在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()&#34;

2 个答案:

答案 0 :(得分:0)

我将我的解决方案升级到最新的Web API版本,这标记为obolete,但有效。

    [HttpGet, Route("Get")]
    public PageResult<DiplomateModel> Get(int legalValue, string area =                                                     null, int? pageSize = 20,ODataQueryOptions<Model> options = null)
    {
        try
        {
            var ret = GetUsers(legalValue, area);
            var settings = new ODataQuerySettings
            {
                PageSize = pageSize
            };
            IQueryable results = options.ApplyTo(ret, settings);
            var uri = Request.GetNextPageLink();
            long? inLineCount = Request.GetInlineCount();
            var response = new PageResult<DiplomateModel>(results as IEnumerable<Model>,uri,inLineCount);
            return response;
        }
        catch (Exception ex)
        {
            LogError(ex);
            return null;
        }
    }

答案 1 :(得分:0)

哦,是的,你的代码不起作用的唯一原因是因为[EnableQuery]装饰器。删除它,你应该很高兴去。 EnableQuery()将阻止您的通话。 EnableQuery仅保护方法免受恶意请求的影响,但如果您的签名是强类型的,则不应该是一个问题。