输出缓存为ApiController(MVC4的Web API) [英] Output caching for an ApiController (MVC4 Web API)
问题描述
我想缓存一个 ApiController 在 MVC4的Web API 。
下面是控制器code:
Here's the controller code:
using System.Web.Http;
using System.Web.Mvc;
using System.Web.UI;
public class TestController : ApiController
{
[OutputCache(Duration = 10, VaryByParam = "none", Location = OutputCacheLocation.Any)]
public string Get()
{
return System.DateTime.Now.ToString();
}
}
N.B。我还尝试了其参数的控制器本身上的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:
using System;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
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屋!