0

I am working on this Sitecore project and am using WebApi to perform some service calls. My methods are decorated with CacheOutput information like this:

[HttpGet]
[CacheOutput(ClientTimeSpan = 3600, ServerTimeSpan = 3600)]

I am testing these calls using DHC app on Google Chrome. I am sure that the ClientTimespan is set correctly but the response headers that i am getting back are not what i am expecting. I would expect that Cache-Control would have a max-age of 1hour as set by the ClientTimespan attribute but instead it is set to private.

enter image description here

I have been debugging everything possible and t turns out that Sitecore may be intercepting the response and setting this header value to private. I have also added the service url to the sitecore ignored url prefixes configuration but no help .

Does anyone have an idea how I can make Sitecore NOT change my Cache-Control headers?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
alinulms
  • 509
  • 1
  • 4
  • 19

1 Answers1

1

This is default MVC behaviour and not directly related to Sitecore / Web API.
You can create a custom attribute that sets the Cache-Control header:

public class CacheControl : System.Web.Http.Filters.ActionFilterAttribute
{
    public int MaxAge { get; set; }

    public CacheControl()
    {
        MaxAge = 3600;
    }

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

        base.OnActionExecuted(context);
    }
}

Which enables you to add the [CacheControl(MaxAge = n)] attribute to your methods.
Code taken from: Setting HTTP cache control headers in WebAPI (answer #2)

Or you can apply it globally throughout the application, as explained here: http://juristr.com/blog/2012/10/output-caching-in-aspnet-mvc/

Community
  • 1
  • 1
Ruud van Falier
  • 8,765
  • 5
  • 30
  • 59
  • Well i am doing it exactly the same way. And the max-age is set correctly. I can see it in debug but then when response comes it is still set to private. I have to say i am not using MVC but i am not sure if that affects it in any way. – alinulms Jun 27 '14 at 10:45