繁体   English   中英

ApiController的输出缓存(MVC4 Web API)

[英]Output caching for an ApiController (MVC4 Web API)

我正在尝试在Web API中缓存ApiController方法的输出。

这是控制器代码:

public class TestController : ApiController
{
    [OutputCache(Duration = 10, VaryByParam = "none", Location = OutputCacheLocation.Any)]
    public string Get()
    {
        return System.DateTime.Now.ToString();
    }
}

NB我还尝试了控制器本身的OutputCache属性,以及它的几个参数组合。

该路线在Global.asax中注册:

namespace WebApiTest
{
    public class Global : HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.MapHttpRoute("default", routeTemplate: "{controller}");
        }
    }
}

我得到了一个成功的回复,但它没有缓存在任何地方:

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中找到输出缓存的文档。

这是MVC4中Web API的限制还是我做错了什么?

WebAPI没有对[OutputCache]属性的任何内置支持。 看看这篇文章 ,了解如何自己实现此功能。

Aliostad的答案表明Web API关闭了缓存,而HttpControllerHandler的代码显示它确实在响应时.Headers.CacheControl为null。

要使您的示例ApiController Action返回可缓存的结果,您可以:

using System.Net.Http;

public class TestController : ApiController
{
    public HttpResponseMessage Get()
    {
        var response = Request.CreateResponse(HttpStatusCode.OK);
        response.Content = new StringContent(System.DateTime.Now.ToString());
        response.Headers.CacheControl = new CacheControlHeaderValue();
        response.Headers.CacheControl.MaxAge = new TimeSpan(0, 10, 0);  // 10 min. or 600 sec.
        response.Headers.CacheControl.Public = true;
        return response;
    }
}

你会得到一个像这样的HTTP响应头:

Cache-Control: public, max-age=600
Content-Encoding: gzip
Content-Type: text/plain; charset=utf-8
Date: Wed, 13 Mar 2013 21:06:10 GMT
...

在过去的几个月里,我一直致力于ASP.NET Web API的HTTP缓存。 我为WebApiContrib贡献了服务器端,相关信息可以在我的博客上找到。

最近我开始扩展工作并在CacheCow库中添加客户端。 第一批NuGet套餐现已发布(感谢Tugberk )更多内容。 我很快就会写一篇博文。 所以看空间。


但为了回答您的问题,ASP.NET Web API默认关闭缓存。 如果希望缓存响应,则需要将CacheControl标头添加到控制器中的响应中(实际上最好是在类似于CacheCow中的CachingHandler的委托处理程序中)。

这个片段来自ASP.NET Web Stack源代码中的HttpControllerHandler

        CacheControlHeaderValue cacheControl = response.Headers.CacheControl;

        // TODO 335085: Consider this when coming up with our caching story
        if (cacheControl == null)
        {
            // DevDiv2 #332323. ASP.NET by default always emits a cache-control: private header.
            // However, we don't want requests to be cached by default.
            // If nobody set an explicit CacheControl then explicitly set to no-cache to override the
            // default behavior. This will cause the following response headers to be emitted:
            //     Cache-Control: no-cache
            //     Pragma: no-cache
            //     Expires: -1
            httpContextBase.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }

我很晚,但仍然想在WebApi上发布这篇关于缓存的文章

https://codewala.net/2015/05/25/outputcache-doesnt-work-with-web-api-why-a-solution/

public class CacheWebApiAttribute : ActionFilterAttribute
{
    public int Duration { get; set; }

    public override void OnActionExecuted(HttpActionExecutedContext filterContext)
    {
        filterContext.Response.Headers.CacheControl = new CacheControlHeaderValue()
        {
            MaxAge = TimeSpan.FromMinutes(Duration),
            MustRevalidate = true,
            Private = true
        };
    }
}

在上面的代码中,我们重写了OnActionExecuted方法并在响应中设置了所需的标头。 现在我将Web API调用装饰为

[CacheWebApi(Duration = 20)]
        public IEnumerable<string> Get()
        {
            return new string[] { DateTime.Now.ToLongTimeString(), DateTime.UtcNow.ToLongTimeString() };
        }

您可以在常规MVC控制器上使用它:

[OutputCache(Duration = 10, VaryByParam = "none", Location = OutputCacheLocation.Any)]
public string Get()
{
    HttpContext.Current.Response.Cache.SetOmitVaryStar(true);
    return System.DateTime.Now.ToString();
}

但OutputCache属性位于System.Web.Mvc命名空间中,在ApiController中不可用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM