2

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

12
  • Have you inspected the headers to see what Web Api is returning? Commented Jul 18, 2014 at 8:50
  • @DavidG I have added the capture of the headers. Commented Jul 18, 2014 at 8:57
  • I'm note sure output caching in the Web API is as simple as setting the header, someone has already went to the liberty of making a library that supports it. Commented Jul 18, 2014 at 8:58
  • @jvrdelafuente Your browser is sending cache-control: max-age=0 which is effectively telling the server My data is stale, give me new stuff Commented Jul 18, 2014 at 9:00
  • 1
    The library linked by @James looks pretty feature complete, I'd give that a go first. Commented Jul 18, 2014 at 9:06

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.