簡體   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