【问题标题】:How to change headers for specific types of files served in IIS / MVC如何更改 IIS / MVC 中提供的特定类型文件的标头
【发布时间】:2011-11-22 16:17:27
【问题描述】:

我正在对 3G 上的 02 压缩问题引起的问题进行修复。

Web site exhibits JavaScript error on iPad / iPhone under 3G but not under WiFi

最好的解决方案似乎是http://stuartroebuck.blogspot.com/2010/08/official-way-to-bypassing-data.html,它基本上是在 IIS 中添加标头Cache-Control: no-transform,但是我想仅将其应用于特定的文件类型。最简单的方法是什么?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 iis http-headers


    【解决方案1】:

    写一个HttpModule对我来说是最好的解决方案。我为你写了一个例子。 您可以检查特定文件类型的 MIME 类型。

    public class AddHeaderModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.EndRequest += OnEndRequest;
        }
    
        void OnEndRequest(object sender, System.EventArgs e)
        {
            if(HttpContext.Current.Response.ContentType == "image/jpeg")
                HttpContext.Current.Response.Headers.AddHeader("Cache-Control", "no-transform");
        }
    }
    

    另外你必须添加它 web.config

    <configuration>
       <system.web>
          <httpModules>
             <add name="AddHeaderModule" type="your.namespace.AddHeaderModule" />
          </httpModules>
       </system.web>
    </configuration>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 2013-01-29
      • 1970-01-01
      相关资源
      最近更新 更多