【问题标题】:Error 500 "Queries cannot be applied to the response content of type 'System.Net.Http.ByteArrayContent'错误 500“查询不能应用于 'System.Net.Http.ByteArrayContent' 类型的响应内容
【发布时间】:2017-05-07 19:09:42
【问题描述】:

我正在尝试在我的 Webapi 中输出缓存。根据https://github.com/filipw/Strathweb.CacheOutput 下方文章中的讨论,我已经安装了 Nuget 包“Strathweb.CacheOutput.WebApi2”。安装后我使用:

[CacheOutput(ClientTimeSpan = 100, ServerTimeSpan = 100, AnonymousOnly = true)]
    public HttpResponseMessage Get()
    {
        var list = context.Colors.ToList();
        return Request.CreateResponse(HttpStatusCode.OK,list.AsQueryable<Color>() );
    }

它返回此错误:

{"Message":"发生错误。","ExceptionMessage":"查询不能应用于'System.Net.Http.ByteArrayContent'类型的响应内容。响应内容必须是ObjectContent。 \r\n参数名称:在 System.Web.Http.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext) 处的 actionExecutedContext","ExceptionType":"System.ArgumentException","StackTrace":"\r\n 在 System.Web.Http .Filters.ActionFilterAttribute.OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancelToken)\r\n--- 堆栈跟踪从先前引发异常的位置结束 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()\r\n--- 堆栈结束从先前引发异常的位置跟踪 ---\r\n 在 System.Ru ntime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext()\ r\n--- 从先前引发异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter .HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- 从先前引发异常的位置结束堆栈跟踪 ---\r\ n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Web.Http.Controllers.AuthenticationFilterResult.d__0。 MoveNext()\r\n--- 从先前引发异常的位置结束堆栈跟踪 ---\r\n 在 Sys tem.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext( )"}

我可以做些什么来解决这个问题。我是 Web Api 的新手。谢谢

【问题讨论】:

    标签: c# asp.net-web-api odata response httpresponse


    【解决方案1】:

    由于 Web Api 是服务器端链中的最后一步,因此需要在将 IQueryable 与响应一起发送之前执行/评估它。所以简单的解决方案就是按原样发送列表

    public HttpResponseMessage Get()
    {
        var list = context.Colors.ToList();
        return Request.CreateResponse(HttpStatusCode.OK,list);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-04
      • 1970-01-01
      • 2015-09-02
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多