繁体   English   中英

如何让 IIS 从 WebApi 2 返回自定义缓存控件 Header,IIS 10 每次返回私有

[英]How do I get IIS to return a Custom-Cache Control Header from WebApi 2, IIS 10 returns private every time

** 2021 年 1 月 27 日更新 ** 我一直在与 Microsoft 合作解决此问题,我们进行了请求跟踪并查看了 FREB 日志并确认代码输出了正确的标头,但在 END_REQUEST 处理程序中它们被替换为缓存-控制私人。 在从头开始构建几个虚拟机之后,我们了解到开箱即用,这个问题不会发生。 但是,当我们安装最新版本的 STACKIFY AGENT (4.29.29) 时,一旦我们把代理放在这种行为上就会发生。 旧版本的 Stackify 代理(RETRACE 分析器和 APM)不这样做,但到目前为止,这是不可逆转的:一旦我们安装了 Stackify 的 4.29.29,卸载或安装旧版本并不能解决这个问题,环境不知何故永久修改。

STACKIFY 回应了一个可行的解决方案(但建议卸载后留下一些东西):设置环境变量 STACKIFY_ENABLERUM = false .. 我们尝试了这个,IIS 返回了我们正确的 header 而不用缓存控制替换它:私有。


我想使用 CloudFront CDN 将流量缓存到我的 API,以便将请求卸载到内容交付网络。

我正在尝试将 Cache-Control header 设置为返回:Cache-Control:“public, max-age=10”。 当我在 Visual Studio 2019 中运行我的代码进行调试时,我得到了正确的 header。 但是,当我将它部署到 IIS 时,我总是会返回:

缓存控制:私有

我正在使用 IIS 版本 10.0.17763.1 运行 Windows Server 2019。 I am using ASP.NET MVC with Web API 2 to operate a REST API.

在我的代码中,我创建了这个属性:

代码:

 public class CacheControlAttribute : System.Web.Http.Filters.ActionFilterAttribute
    {
        public int MaxAge { get; set; }

        public CacheControlAttribute()
        {
            MaxAge = 0;
        }

        public override void OnActionExecuted(HttpActionExecutedContext context)
        {
            // Don't set the cache header if there was an error or if there was no content in the response
            if (context.Response != null && context.Response.IsSuccessStatusCode)
            {
                context.Response.Headers.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue
                {
                    Private = false,
                    Public = true,
                    MaxAge = TimeSpan.FromSeconds(MaxAge)
                };                

            }

            base.OnActionExecuted(context);
        }
    }

然后我用这个属性装饰我的方法:

[CacheControl(MaxAge = 10)]

我阅读了几篇关于 StackOverflow 的文章,但无法解决这个问题。 我还尝试将其添加到 web.config 中:

它没有帮助。

我在一台 IIS 服务器上成功完成了这项工作,IIS 版本相同,但我的代码版本较旧。 我在新的 IIS 服务器上设置它时遇到问题,我认为它是 IIS 配置问题,但我不确定。 也许它在我的 web.config 中。

感谢您对此的任何帮助。

缓存控制分为请求和响应指令。 我猜你说的是响应中的缓存控制,因为请求缓存控制不能在 IIS 中设置。

出于安全考虑,IIS默认会设置cache-control为private,所以你会遇到这个问题。 当没有用于请求的 output 缓存(并且您启用了 output 缓存)时,这是 .NET 的默认行为。 如果在 web.config 中将 sendCacheControlHeader 设置为 false,则不会获得 Cache-Control: private header。

因此,一种简单的方法是将 sendCacheControlHeader 设置为 false 并添加缓存控制将删除私有。 您也不需要在 MVC 中自定义缓存控制过滤器。

<system.web>
   <httpRuntime sendCacheControlHeader="false" /> 
</system.web>

<httpProtocol>
 <customHeaders>
   <remove name="X-Powered-By" />
    <add name="Cache-Control" value="max-age=30,public" />
 </customHeaders>
</httpProtocol>

另一个想法是为每个动作或 controller 添加缓存的 output。 .NET 有一个定义的过滤器来控制maxage,你不需要自己定义它。 您可以使用[OutputCache(Duration = 0)]

输出缓存

您需要像这样在现有的 output 缓存之上实现自己的

public static class MyCustomCacheConfig
{
    public static int Duration = 600; //whatever time you want
}

public class CdnCacheCacheAttribute : OutputCacheAttribute
{
    public CdnCacheCacheAttribute()
    {
        this.Duration = MyCustomCacheConfig.Duration;
    }
}

[CdnCacheCacheAttribute(Duration = 100, VaryByParam = "none")]
public ActionResult YourActions()
{
    return View();
}

来自 Microsoft 的 output 缓存中的VaryByParm控制HttpCaching

// Get the current VaryByParam.
String varyByParamValue =  outputCacheProfile.VaryByParam;

// Set the VaryByParam.outputCacheProfile.VaryByParam = string.Empty;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM