从IIS中删除Etag和Last-Modified标头

时间:2010-10-11 14:20:17

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

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

当然,这在Apache中很容易,但在IIS 6中很明显。有人知道如何在IIS中删除这两个标题吗?

1 个答案:

答案 0 :(得分:7)

一种编程方式是使用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安装,位于{6}的<system.web>下,IIS 7的<system.webServer>下。

相关问题