1

I am trying to set the Cache-Control header on the response for GET request.

This works, with OPTIONS requests:

PreRequestFilters.Add((httpRequest, httpResponse) =>
{
   if (httpRequest.HttpMethod == "OPTIONS")
   {
      httpResponse.AddHeader("Cache-Control", "no-cache");
      httpResponse.EndServiceStackRequest();
   }
});

This does not work, with GET requests:

ResponseFilters.Add((httpRequest, httpResponse, dto) =>
{
   httpResponse.AddHeader("Cache-Control", "no-cache");
});

The filters are working... Also I am able to add my own headers to the response using the above method.

I am using 3.9.58.

So, is this a bug (in ServiceStack or in my code), or is this by design because of the nature of REST and GET request ?

Roger Down
  • 73
  • 4
  • What does "does not work" mean? Is the header you set no present in the response when you examine it client-side? Is the header still present but the cached value is being used? – Ray Nicholus Aug 21 '13 at 13:07
  • Sorry, not very clear question on my side. The first example returns the header Cache-Control: no-cache. The second (which does "not work") will return Cache-Control: private. What I want to accomplish is for the second to also return Cache-Control: no-cache. – Roger Down Aug 22 '13 at 09:31

1 Answers1

0

You don't want to do this, which terminates the request:

httpResponse.EndServiceStackRequest();

Which is also deprecated, If you want to short-circuit the request and prevent future processing you should use:

httpResponse.EndRequest();

But in this situation you just want to add a header, you don't want to do this.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thanks for the answer... :) Regarding the deprecated method, I don't see any .EndRequest method on the httpResponse object. Do I need to add some namespace for this ? For OPTIONS in CORS, I want to just add the header and terminate the request. Code is base on an answer you gave here [link] (http://stackoverflow.com/questions/13741397/servicestack-cors-feature). Perhaps the OPTIONS code should go in the ResponseFilter.Add instead ? – Roger Down Aug 22 '13 at 09:24
  • With the CORS example the desired behavior is to respond with the OPTIONS request and send the CORS HTTP Headers and end the request - the service itself shouldn't be executed. I've just updated it to use `EndRequest()` which is an extension method (just as EndServiceStackRequest was) in `ServiceStack` namespace. – mythz Aug 22 '13 at 15:18