Set Cache-Control: no-cache on GET requests

北战南征 提交于 2019-12-13 02:19:32

问题


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:


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.



来源:https://stackoverflow.com/questions/18356002/set-cache-control-no-cache-on-get-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!