简体   繁体   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

** UPDATE Jan 27, 2021 ** I have been working with Microsoft on this issue and we did a request trace and reviewed the FREB log and confirmed that the code is outputting the correct headers but in the END_REQUEST handler they are replaced with cache-control private. ** 2021 年 1 月 27 日更新 ** 我一直在与 Microsoft 合作解决此问题,我们进行了请求跟踪并查看了 FREB 日志并确认代码输出了正确的标头,但在 END_REQUEST 处理程序中它们被替换为缓存-控制私人。 After building several virtual machines from scratch we learned that out of the box, this problem doesn't happen.在从头开始构建几个虚拟机之后,我们了解到开箱即用,这个问题不会发生。 However when we install the latest version of STACKIFY AGENT (4.29.29) once we put the agent on this behavior happens.但是,当我们安装最新版本的 STACKIFY AGENT (4.29.29) 时,一旦我们把代理放在这种行为上就会发生。 Older versions of Stackify agent (RETRACE profiler and APM) don't do this, but so far, this is irreversible: once we install 4.29.29 of Stackify, uninstalling or installing older versions doesn't undo this issue, the environment is somehow permanently modified.旧版本的 Stackify 代理(RETRACE 分析器和 APM)不这样做,但到目前为止,这是不可逆转的:一旦我们安装了 Stackify 的 4.29.29,卸载或安装旧版本并不能解决这个问题,环境不知何故永久修改。

STACKIFY responded with a solution which works (but suggests something is left behind after uninstall): Set the environment variable STACKIFY_ENABLERUM = false.. we tried this and IIS returned our correct header without replacing it with cache-control: private. STACKIFY 回应了一个可行的解决方案(但建议卸载后留下一些东西):设置环境变量 STACKIFY_ENABLERUM = false .. 我们尝试了这个,IIS 返回了我们正确的 header 而不用缓存控制替换它:私有。


I want to use CloudFront CDN to cache traffic to my API, so that requests are offloaded to the content delivery network.我想使用 CloudFront CDN 将流量缓存到我的 API,以便将请求卸载到内容交付网络。

I'm trying to set the Cache-Control header to return: Cache-Control: "public, max-age=10".我正在尝试将 Cache-Control header 设置为返回:Cache-Control:“public, max-age=10”。 When I run my code in Visual Studio 2019 to debug it, I get the correct header.当我在 Visual Studio 2019 中运行我的代码进行调试时,我得到了正确的 header。 However, when I deploy it to IIS, I always get back:但是,当我将它部署到 IIS 时,我总是会返回:

Cache-Control: private缓存控制:私有

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

In my code I created this attribute:在我的代码中,我创建了这个属性:

Code:代码:

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

I then decorate my method with this attribute:然后我用这个属性装饰我的方法:

[CacheControl(MaxAge = 10)]

I read several articles on StackOverflow but was not able to solve this issue.我阅读了几篇关于 StackOverflow 的文章,但无法解决这个问题。 I also tried adding this to web.config:我还尝试将其添加到 web.config 中:

It did not help.它没有帮助。

I have this working successfully on one IIS server, same version of IIS but older version of my code.我在一台 IIS 服务器上成功完成了这项工作,IIS 版本相同,但我的代码版本较旧。 I'm having the problem setting it up on a new IIS server and I think its an IIS configuration issue but I am not certain.我在新的 IIS 服务器上设置它时遇到问题,我认为它是 IIS 配置问题,但我不确定。 Maybe its something in my web.config.也许它在我的 web.config 中。

Thank you for any help with this.感谢您对此的任何帮助。

Cache-control is divided into request and response directives.缓存控制分为请求和响应指令。 I guess you are talking about the cache-control in the response, because the request cache-control cannot be set in IIS.我猜你说的是响应中的缓存控制,因为请求缓存控制不能在 IIS 中设置。

For security reasons, IIS will set cache-control to private by default, so you will encounter this problem.出于安全考虑,IIS默认会设置cache-control为private,所以你会遇到这个问题。 This is default behaviour for .NET when there's no output cache used for a request (and you have output cache enabled).当没有用于请求的 output 缓存(并且您启用了 output 缓存)时,这是 .NET 的默认行为。 If you set the sendCacheControlHeader to false in web.config, you will not get the Cache-Control: private header.如果在 web.config 中将 sendCacheControlHeader 设置为 false,则不会获得 Cache-Control: private header。

So an easy way is set sendCacheControlHeader false and add cache-control will remove private.因此,一种简单的方法是将 sendCacheControlHeader 设置为 false 并添加缓存控制将删除私有。 You also don't need to custom cache-control filter in MVC.您也不需要在 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>

Another idea is to add cached output for each action or controller.另一个想法是为每个动作或 controller 添加缓存的 output。 .NET has a defined filter to control maxage, you don't need to define it yourself. .NET 有一个定义的过滤器来控制maxage,你不需要自己定义它。 You can use [OutputCache(Duration = 0)]您可以使用[OutputCache(Duration = 0)]

OutputCache 输出缓存

You need to implement your own on top of the existing output cache like so您需要像这样在现有的 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();
}

VaryByParm in output cache from Microsoft.. which controls the HttpCaching来自 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.

相关问题 如何通过web.config或IIS设置从Cache-Control删除私有 - How to remove private from Cache-Control through web.config or IIS settings IIS 错误 500.2 -1 如何在 IIS Windows 10 中部署一个简单的自定义处理程序? - IIS Error 500.2 -1 How do I deploy a simple custom Handler in IIS Windows 10? IIS 8.5 覆盖自定义 JSON 错误响应,而是返回默认的 500 错误响应页面。 如何让 IIS 8.5 返回我的自定义错误? - IIS 8.5 overriding custom JSON error response, instead returns the default 500 error response page. How can I get IIS 8.5 to return my custom error? 如何从 IIS 获取站点使用情况? - How do I get site usage from IIS? 如何在每次加载页面时运行jQuery,甚至是从缓存中运行? - How do I get my jQuery to be run every time a page is loaded, even from the cache? 如何在每次更改代码时不重新启动IIS? - How to not restart IIS every time I make changes to code? 如何进入 IIS 管理器? - How do I get to IIS Manager? 无法让 ASP.NET/IIS 为具有自定义缓存控制设置的 .js 文件提供服务 - Cannot get ASP.NET/IIS to serve .js files with custom cache-control settings 如何绕过IIS中的接受标头验证? - How do I bypass Accept header validation in IIS? IIS7缓存控制 - IIS7 Cache-Control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM