ApiController 的输出缓存(MVC4 Web API) [英] Output caching for an ApiController (MVC4 Web API)
问题描述
我正在尝试缓存 的输出Web API 中的 ApiController 方法.
I'm trying to cache the output of an ApiController method in Web API.
这是控制器代码:
public class TestController : ApiController
{
[OutputCache(Duration = 10, VaryByParam = "none", Location = OutputCacheLocation.Any)]
public string Get()
{
return System.DateTime.Now.ToString();
}
}
注意我还尝试了控制器本身的 OutputCache 属性,以及其参数的几种组合.
N.B. I'd also tried the OutputCache attribute on the controller itself, as well as several combinations of its parameters.
路由在 Global.asax 中注册:
The route is registered in Global.asax:
namespace WebApiTest
{
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHttpRoute("default", routeTemplate: "{controller}");
}
}
}
我收到了成功的响应,但它没有缓存在任何地方:
I get a successful response, but it's not cached anywhere:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/xml; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 18 Jul 2012 17:56:17 GMT
Content-Length: 96
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">18/07/2012 18:56:17</string>
我找不到有关 Web API 中输出缓存的文档.
I was not able to find documentation for output caching in Web API.
这是 MVC4 中 Web API 的限制还是我做错了什么?
Is this a limitation of the Web API in MVC4 or am I doing something wrong?
推荐答案
WebAPI 没有对 [OutputCache]
属性的任何内置支持.看看看看这篇文章了解如何自己实现此功能.
WebAPI does not have any built in support for the [OutputCache]
attribute. Take a look at this article to see how you could implement this feature yourself.
这篇关于ApiController 的输出缓存(MVC4 Web API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!