kkatsma
kkatsma

Reputation: 31

ServiceStack overriding Cache-Control private

There's some similar old questions to this, one answered and one unanswered. The answered applies to ServiceStack 3, and the accepted answer does not work for me in 4.0.36

I can create custom response headers with the ResponseFilters, but it's not possible to override Cache-Control - it is always set to private.

public class NoCacheAttribute : ResponseFilterAttribute
{
    public override void Execute(IRequest req, IResponse res, object responseDto)
   {
       res.AddHeader("Cache-Control", "no-store");  //does not work
       res.AddHeader("Test", "no-store");  //works

   }
} 

Likewise, I've also tried to set in the Service implementation itself, using the following, which again only works for custom header. I've used the CacheControl constant and "Cache-Control" string in both methods, makes no difference:

Response.AddHeader(HttpHeaders.CacheControl, "no-store"); //doesn't work
Response.AddHeader("Test2", "no-store");  //works

Has anyone successfully done this in ServiceStack recently? It seems like some lower level ASP.NET function is keeping the Cache-Control set to "private"

Upvotes: 0

Views: 245

Answers (1)

jeffgabhart
jeffgabhart

Reputation: 1127

Here is what I have in v4. I don't recall why I had to switch from ResponseFilter to RequestFilter, but I think it had to do with certain cases ending the request early and the ResponseFilter was skipped.

The RequestFilter makes sure you get the headers onto the IResponse object as soon as possible.

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

Upvotes: 1

Related Questions