我正试图在我的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>() );
}它返回此错误:
{“消息”:“发生错误。”、“ExceptionMessage”:“查询不能应用于'System.Net.Http.ByteArrayContent‘类型的响应内容。响应内容必须是ObjectContent。\r\n参数名称: System.Web.Http.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext“actionExecutedContext”、"ExceptionType":"System.ArgumentException“、"StackTrace":”at System.ArgumentException actionExecutedContext)\r\n at System.Web.Http.Filters.ActionFilterAttribute.OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext,\r)\r\n--从抛出异常的前一个位置开始的堆栈跟踪\r\n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务中)\r\n在堆栈跟踪的System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext()\r\n---末端,从抛出异常的前一个位置开始从抛出异常的前一个位置开始的堆栈跟踪的结束(-\r\n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任务中)\r\n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务中)\r\n在堆栈跟踪的System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()\r\n---端从抛出异常的位置开始-\r\n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task上\r\n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务)\r\n在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}任务
我能做些什么来解决这个问题。我是Web的新手。谢谢
发布于 2016-12-22 08:49:12
由于Web是服务器端链中的最后一步,所以在发送响应之前,需要执行/评估IQueryable。因此,简单的解决方案就是按原样发送列表。
public HttpResponseMessage Get()
{
var list = context.Colors.ToList();
return Request.CreateResponse(HttpStatusCode.OK,list);
}https://stackoverflow.com/questions/41278387
复制相似问题