5

您是否知道可以通过完全删除 ETag 和 Last-Modifed 响应标头来防止重新验证浏览器缓存中的文件和随后的 304 响应?

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

4

1 回答 1

7

一种编程方式是使用 HTTP 模块,如下所示(基于Luke 的 SO 回答):

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 在<system.web>IIS 6 和<system.webServer>IIS 7 下安装。

于 2010-10-11T14:32:09.127 回答