【问题标题】:Using PageResult in a base ApiController returns a System.ArgumentNullException在基础 ApiController 中使用 PageResult 会返回 System.ArgumentNullException
【发布时间】:2014-12-30 20:31:56
【问题描述】:

我正在尝试编写一个已添加到基本控制器的通用 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

我收到一个错误

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

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

我还应该做些什么来启用它吗?参数名source....不知道这是什么?

完整的堆栈跟踪

" 在 System.Linq.Queryable.Count[TSource](IQueryable1 source)\ \ at Api.EndPoints.BaseController5.Get(ODataQueryOptions1 options) in e:\\@Source\\Web\\Api\\.EndPoints\\BaseController.cs:line 62\ \ at lambda_method(Closure , Object , Object[] )\ \ at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.&lt;&gt;c__DisplayClass10.&lt;GetExecutor&gt;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, IDictionary2 arguments, CancellationToken cancelToken)\ \ --- 堆栈跟踪从之前抛出异常的位置结束---\ \ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ \ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ \ at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\ \ at System.Web.Http.Controllers.ApiControllerActionInvoker.&lt;InvokeActionAsyncCore&gt;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.TaskAwaiter1。 GetResult()\ \ at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()\ \ --- 从先前抛出异常的位置结束堆栈跟踪---\ \ at System.Runtime.ExceptionServices.ExceptionDispatchInfo .Throw()\ \ at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()\ \ --- 从先前引发异常的位置结束堆栈跟踪 ---\ \ 在 System.Runtime.CompilerServices。 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndD 处的 TaskAwaiter.ThrowForNonSuccess(任务任务)\ \ ebuggerNotification(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\ \ at System.Web.Http.Filters.ActionFilterAttribute.&lt;ExecuteActionFilterAsyncCore&gt;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.TaskAwaiter1.GetResult()\ \ at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\ \ --- 堆栈跟踪结束从先前引发异常的位置---\ \ 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.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"

【问题讨论】:

  • 这是因为在ApplyTo方法之后,结果不是IQueryable类型,而是TListModel的部分项目集合。

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我将我的解决方案升级到了最新的 Web API 版本,这被标记为过时,但可以正常工作。

        [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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-18
      • 2018-01-02
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多