jvrdelafuente
jvrdelafuente

Reputation: 2002

Caching Asp .Net Web Api

I am trying to catch an method in the api that I am implementing. I have created an attribute that is based in this article(http://www.codeproject.com/Articles/682296/Setting-Cache-Control-HTTP-Headers-in-Web-API-Cont).

[Route("")]
[CacheControl(MaxAge = 160)]
public IEnumerable<Club> GetAll()
{
    return _clubService.GetAll();
}

The code of this attribute is:

public class CacheControlAttribute : ActionFilterAttribute
{
    public int MaxAge { get; set; }

    public CacheControlAttribute()
    {
        MaxAge = 160;
    }

    public override void OnActionExecuted(HttpActionExecutedContext context)
    {
        context.Response.Headers.CacheControl = new CacheControlHeaderValue
        {
            Public = true,
            MaxAge = TimeSpan.FromSeconds(MaxAge)
        }; 

        base.OnActionExecuted(context);
    }
}

I am calling directly this method from chrome. But the server is always executing the query and I canot get the server to return Not Modified. I am doing something wrong?

---------------------------------------------------------------EDIT-------------------------------------------------------------- These are the header of my call:

enter image description here

Upvotes: 2

Views: 1579

Answers (1)

James
James

Reputation: 82096

Output caching is not currently supported by the Web API, however, someone has already gone to the trouble of building a library that does exactly what you need - AspNetWebApi-OutputCache.

Upvotes: 1

Related Questions