How to change headers for specific types of files served in IIS / MVC

℡╲_俬逩灬. 提交于 2020-01-15 03:30:15

问题


I am implementing a fix for the problem caused by 02 compression issues over 3G.

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

The best solution seems to be http://stuartroebuck.blogspot.com/2010/08/official-way-to-bypassing-data.html which is basically to add the header Cache-Control: no-transform in IIS, however I would like to apply this only to specific file types. What is the easiest way to do this?


回答1:


It is the best solution for me to write a HttpModule. I wrote an example for you. You can check mime type for specific file types.

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");
    }
}

Also you have to add it web.config

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


来源:https://stackoverflow.com/questions/8230001/how-to-change-headers-for-specific-types-of-files-served-in-iis-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!