【问题标题】:ServiceStack overriding Cache-Control privateServiceStack 覆盖 Cache-Control 私有
【发布时间】:2015-03-25 04:11:25
【问题描述】:

有一些与此类似的老问题,一个已回答,一个未回答。答案适用于 ServiceStack 3,而接受的答案在 4.0.36 中对我不起作用

我可以使用 ResponseFilters 创建自定义响应标头,但无法覆盖 Cache-Control - 它始终设置为私有。

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

   }
} 

同样,我也尝试在服务实现本身中设置,使用以下内容,这同样只适用于自定义标头。我在这两种方法中都使用了 CacheControl 常量和“Cache-Control”字符串,没有区别:

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

最近有没有人在 ServiceStack 中成功地做到了这一点?似乎一些较低级别的 ASP.NET 函数将 Cache-Control 设置为“私有”

【问题讨论】:

  • IIS 是否配置为设置该标头?通常在 IIS 网站中预定义一个标头(“X-Powered-By”),但缓存控制标头可能是硬编码的?

标签: servicestack cache-control


【解决方案1】:

这是我在 v4 中的内容。 我不记得为什么我必须从ResponseFilter 切换到RequestFilter,但我认为这与某些情况下提前结束请求和跳过ResponseFilter 有关。

RequestFilter 确保您尽快将标头放到IResponse 对象上。

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");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 2015-07-01
    • 2018-03-14
    • 1970-01-01
    相关资源
    最近更新 更多