【问题标题】:Remove Etag and Last-Modified headers from IIS从 IIS 中删除 Etag 和 Last-Modified 标头
【发布时间】:2011-04-23 20:25:22
【问题描述】:

您知道您可以通过完全删除 ETag 和 Last-Modifed 响应标头来prevent the revalidation of files in browser cache and subsequent 304 response 吗?

当然,这在 Apache 中很容易,但在 IIS 6 中则一清二楚。有谁知道如何在 IIS 中删除这两个标头?

【问题讨论】:

    标签: caching iis-6 last-modified etag http-status-code-304


    【解决方案1】:

    一种编程方式是使用 HTTP 模块,类似这样(基于SO answer by Luke):

    namespace HttpModules
    {
        using System;
        using System.Web;
    
        public class RemoveExtraneousHeaderModule : IHttpModule
        {
            /// <summary>
            /// Initializes a module and prepares it to handle requests.
            /// </summary>
            /// <param name="context">Provides access to the request context.</param>
            public void Init(HttpApplication context)
            {
                context.PreSendRequestHeaders += this.OnPreSendRequestHeaders;
            }
    
            /// <summary>
            /// Disposes of the resources (other than memory) used by this module.
            /// </summary>
            public void Dispose()
            {
            }
    
            /// <summary>
            /// Event raised just before ASP.NET sends HTTP headers to the client.
            /// </summary>
            /// <param name="sender">Event source.</param>
            /// <param name="e">Event arguments.</param>
            protected void OnPreSendRequestHeaders(object sender, EventArgs e)
            {
                NameValueCollection headers = HttpContext.Current.Response.Headers;
                headers.Remove("Server");
                headers.Remove("ETag");
                headers.Remove("X-Powered-By");
                headers.Remove("X-AspNet-Version");
                headers.Remove("X-AspNetMvc-Version");
            }
        }
    }
    

    该模块通过 web.config 安装,在 IIS 6 下位于 &lt;system.web&gt; 下,在 IIS 7 下位于 &lt;system.webServer&gt; 下。

    【讨论】:

    • 谢谢,这很有用。但是,IIS 添加标头,然后让一些代码再次删除它们似乎效率低下。我真的更喜欢“从源头”删除它们。有什么好主意吗? IIS 并不容易。
    • 这些标头是由 IIS 核心代码添加的,其设计与 Apache 的功能公开方式不同。使用这样的模块非常快,可能比使用较低级别的标头管理功能更快。
    • 非常同意。我研究了使用 Isapi 过滤器删除标头,但这似乎很神秘,而且关于它的信息很少。我们最近采用 Akamai 作为内容交付网络,我正在研究在此级别上操作标头,从而提供更精细的控制。本质上,问题仍然是 IIS 缺乏控制。
    • 这在 IIS6 中如何工作,当 Response.Headers 抛出并且 InvalidOperationException 如果不是在集成模式下运行时?
    猜你喜欢
    • 2010-10-23
    • 2011-12-18
    • 2019-09-25
    • 2017-12-09
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多