How can I override [ResponseCache] attribute property later in the action method?

404 Views Asked by At

I have a [ResponseCache] attribute on an action method with the default cache Location = ResponseCacheLocation.Any. But in few cases only I want to override this property to ResponseCacheLocation.Client.

Is there a better way to override the attribute in the action method code than using Response.Headers.Add ? (not even sure if it would work)

Using DefaultHttpContext in the unit test I can access to GetTypedHeaders, but I don't have access to this method from the real Response in the controller.

1

There are 1 best solutions below

0
On

I can confirm that the [ResponseCache] attribute can be overridden within the action method in the following way:

HttpContext.Response.Headers["Cache-Control"] = "private,no-cache, no-store, max-age=0";

However, I don't think there's a better way to handle these kind of "exceptions to the rule".