2016-12-22 24 views
3

我想輸出緩存在我的Webapi中我根據討論安裝了Nuget包」Strathweb.CacheOutput.WebApi2「 。下面https://github.com/filipw/Strathweb.CacheOutput文章中安裝後,我使用:錯誤500「查詢不能應用到'System.Net.Http.ByteArrayContent'類型的響應內容'

[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()"}

我能做些什麼來解決這個問題,我是新與網頁API感謝

回答

3

由於Web Api是服務器端鏈中的最後一步,因此需要在發送響應之前執行/評估IQueryable。所以簡單的解決方案只是發送清單,因爲它是

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