Set Cache-Control: no-cache on GET requests

2019-08-02 03:16发布

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 ?

1条回答
爷、活的狠高调
2楼-- · 2019-08-02 04:04

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.

查看更多
登录 后发表回答