【发布时间】: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](IQueryable
1 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.<>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, 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.<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.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.<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.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