Rich Dudley
Rich Dudley

Reputation: 73

Overriding Cache-control: Private in ServiceStack

This is similar to question Set Cache-Control: no-cache on GET requests, which wasn't really answered.

In the API responses, the cache-control header is being set to private, and I'm pretty sure I need No-Cache. I tried a ResponseFilter, but the value didn't change. It's not obvious where this is being added or set in the code or how to override, so any guidance would be appreciated.

Upvotes: 4

Views: 928

Answers (2)

jeffgabhart
jeffgabhart

Reputation: 1127

I use a request filter attribute that I apply to each service or method that should not be cached on the client.

public class NoCacheAttribute : RequestFilterAttribute
{
    public override void Execute(IHttpRequest req, IHttpResponse res, object responseDto)
    {
        res.AddHeader(HttpHeaders.CacheControl, "no-store,must-revalidate,no-cache,max-age=0");
    }
}

Upvotes: 7

Christian Hagelid
Christian Hagelid

Reputation: 8355

In our current implementation we are setting this manually in each service using Response.AddHeader(...). This is because we want to control the cache expiration time per service. I would be interested in learning cleaner ways of doing this though.

Response.AddHeader(ServiceStack.Common.Web.HttpHeaders.CacheControl, String.Format("max-age={0}, public", ConfigurationManager.AppSettings["Cache.Expiration.Resource"]));

Upvotes: 2

Related Questions