在ASP.NET中使用gzip压缩内容文件MVC 3 [英] GZipping content files in ASP.NET MVC 3
问题描述
我用下面的属性来装饰我的 BaseController
类。
I use the following attribute to decorate my BaseController
class.
public class OutputCompressAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
if (string.IsNullOrEmpty(encodingsAccepted))
return;
encodingsAccepted = encodingsAccepted.ToLowerInvariant();
HttpResponseBase response = filterContext.HttpContext.Response;
if (encodingsAccepted.Contains("gzip"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
else if (encodingsAccepted.Contains("deflate"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
}
}
问题是,即使这工作得很好的意见和每一个动作的结果,该属性不工作了 /内容
项目的文件夹的东西。我不知道我怎么可能让这个在内容文件
文件夹使用控制器,或东西,让我这些过滤器追加到响应在某种程度上约束或钩住头。
The issue is that, even though this works just fine for views and every action result, the attribute isn't working for stuff on the /Content
folder of the project. I was wondering how I could make it so that files in the Content
folder use a controller, or are bound somehow or hooked by something that allows me to append these filters to the response header.
推荐答案
您可以的激活COM pression 在IIS。你可以对静态和动态内容做到这一点。
Instead of writing such action filters and reinventing the wheels you could activate compression in IIS. You could do this for static and dynamic content.
这篇关于在ASP.NET中使用gzip压缩内容文件MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!