Output caching for an ApiController (MVC4 Web API)

蹲街弑〆低调 提交于 2019-11-27 18:42:16
Cory

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.

Luciano Carvalho

The answer of Aliostad states that Web API turns off caching, and the code of HttpControllerHandler shows that it does WHEN response.Headers.CacheControl is null.

To make your example ApiController Action return a cacheable result, you can:

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;
    }
}

and you will get a HTTP response header like this:

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
...
Aliostad

For the last few months, I have been working on HTTP caching for ASP.NET Web API. I have contributed to WebApiContrib for server-side and relevant information can be found on my blog.

Recently I have started to expand the work and add the client-side as well in the CacheCow library. First NuGet packages have been released now (thanks to Tugberk) More to come. I will write a blog post soon on this. So watch the space.


But in order to answer your question, ASP.NET Web API by default turns off the caching. If you want the response to be cached, you need to add the CacheControl header to the response in your controller (and in fact better be in a delegating handler similar to CachingHandler in CacheCow).

This snippet is from HttpControllerHandler in ASP.NET Web Stack source code:

        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);
        }

I am very late, but still thought to post this great article on Caching in 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
        };
    }
}

In the above code, we have overridden OnActionExecuted method and set the required header in the response. Now I have decorated the Web API call as

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

You could use this on a regular MVC Controller:

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

but OutputCache attribute is in System.Web.Mvc namespace and not available in an ApiController.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!