GZipping content files in ASP.NET MVC 3

匿名 (未验证) 提交于 2019-12-03 02:21:02

问题:

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.

回答1:

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.



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