how to compress the output send by an asp.net mvc application??
如何压缩asp.net mvc应用程序发送的输出?
3 个解决方案
#1
83
Here's what i use (as of this monent in time):
以下是我所使用的(就像这个月一样):
using System.IO.Compression;
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);
}
}
}
usage in controller:
使用控制器:
[Compress]
public class BookingController : 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.
还有其他的变量,但这很管用。顺便说一句,我在BaseController上使用[Compress]属性在整个项目中保存副本,而上面的方法是在控制器的基础上进行。
[Edit] 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:
[编辑]如上文第a段所述。为了简化使用,您还可以在BaseController本身中包含[Compress] oneshot,因此,每个继承的子控制器默认访问该功能:
[Compress]
public class BaseController : Controller
{...}
#2
5
Have a look at this article which outlines a nifty method utilizing Action Filters
看看这篇文章,它概述了一种利用动作过滤器的绝妙方法
http://weblogs.asp.net/rashid/archive/2008/03/28/asp-net-mvc-action-filter-caching-and-compression.aspx
E.g.
如。
[CompressFilter]
public void Category(string name, int? page)
And as an added bonus, it also includes a CacheFilter
此外,它还包括一个CacheFilter(缓存过滤器)功能
#3
-1
You can also increase the performance by using compression and caching for the response data. Have a look at the following link :-
您还可以通过对响应数据使用压缩和缓存来提高性能。请看以下链接:-
http://weblogs.asp.net/rashid/asp-net-mvc-action-filter-caching-and-compression
http://weblogs.asp.net/rashid/asp-net-mvc-action-filter-caching-and-compression
#1
83
Here's what i use (as of this monent in time):
以下是我所使用的(就像这个月一样):
using System.IO.Compression;
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);
}
}
}
usage in controller:
使用控制器:
[Compress]
public class BookingController : 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.
还有其他的变量,但这很管用。顺便说一句,我在BaseController上使用[Compress]属性在整个项目中保存副本,而上面的方法是在控制器的基础上进行。
[Edit] 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:
[编辑]如上文第a段所述。为了简化使用,您还可以在BaseController本身中包含[Compress] oneshot,因此,每个继承的子控制器默认访问该功能:
[Compress]
public class BaseController : Controller
{...}
#2
5
Have a look at this article which outlines a nifty method utilizing Action Filters
看看这篇文章,它概述了一种利用动作过滤器的绝妙方法
http://weblogs.asp.net/rashid/archive/2008/03/28/asp-net-mvc-action-filter-caching-and-compression.aspx
E.g.
如。
[CompressFilter]
public void Category(string name, int? page)
And as an added bonus, it also includes a CacheFilter
此外,它还包括一个CacheFilter(缓存过滤器)功能
#3
-1
You can also increase the performance by using compression and caching for the response data. Have a look at the following link :-
您还可以通过对响应数据使用压缩和缓存来提高性能。请看以下链接:-
http://weblogs.asp.net/rashid/asp-net-mvc-action-filter-caching-and-compression
http://weblogs.asp.net/rashid/asp-net-mvc-action-filter-caching-and-compression