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