gpt4 book ai didi

c# - 错误 500 "Queries cannot be applied to the response content of type ' System.Net.Http.ByteArrayContent'

转载 作者:行者123 更新时间:2023-11-30 15:59:42 25 4
gpt4 key购买 nike

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

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

它返回这个错误:

{"Message":"An error has occurred.","ExceptionMessage":"Queries can not be applied to a response content of type 'System.Net.Http.ByteArrayContent'. The response content must be an ObjectContent.\r\nParameter name: actionExecutedContext","ExceptionType":"System.ArgumentException","StackTrace":" at System.Web.Http.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)\r\n at System.Web.Http.Filters.ActionFilterAttribute.OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}

我该怎么做才能解决这个问题。我是 Web Api 的新手。谢谢

最佳答案

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

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

关于c# - 错误 500 "Queries cannot be applied to the response content of type ' System.Net.Http.ByteArrayContent',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41278387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com