如何gzip压缩在asp.net MVC的内容? [英] how to gzip content in asp.net MVC?
问题描述
如何COM preSS通过一个asp.net MVC应用程序?
how to compress the output send by an asp.net mvc application??
推荐答案
下面是我用什么(因为这矩估计的时间):
Here's what i use (as of this monent in time):
public class CompressAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
if (string.IsNullOrEmpty(encodingsAccepted)) return;
encodingsAccepted = encodingsAccepted.ToLowerInvariant();
var response = filterContext.HttpContext.Response;
if (encodingsAccepted.Contains("deflate"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
else if (encodingsAccepted.Contains("gzip"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
}
}
在使用控制器:
[Compress]
public class BookingController : BaseController
{...}
还有其他的一些变体,但是这个工作得很好。 (顺便说一句,我使用的[Com preSS]属性我BaseController保存在项目重复,而上述由控制器基础做一个控制器上。
there are other varients, but this works quite well. (btw, i use the [Compress] attribute on my BaseController to save duplication across the project, whereas the above is doing it on a controller by controller basis.
如在对上面提到的。为了简化使用,还可以包括 [通信preSS]
单稳在BaseController本身,因此,每一个继承子控制器默认访问功能:
as mentioned in the para above. to simplify usage, you can also include [Compress]
oneshot in the BaseController itself, thereby, every inherited child controller accesses the functionality by default:
[Compress]
public class BaseController : Controller
{...}
这篇关于如何gzip压缩在asp.net MVC的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!