asp.net-mvc 如何在asp.net MVC中gzip内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3802107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:30:55  来源:igfitidea点击:

how to gzip content in asp.net MVC?

asp.net-mvcasp.net-mvc-2gzip

提问by Praveen Prasad

how to compress the output send by an asp.net mvc application??

如何压缩由 asp.net mvc 应用程序发送的输出?

回答by jim tollan

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:

[编辑]如上段所述。为了简化使用,您还可以[Compress]在 BaseController 本身中包含oneshot,从而默认情况下每个继承的子控制器都会访问该功能:

[Compress]
public class BaseController : Controller
{...}

回答by veggerby

Have a look at this articlewhich outlines a nifty method utilizing Action Filters.

看看这篇文章,它概述了一个利用动作过滤器的漂亮方法。

For example:

例如:

[CompressFilter]
public void Category(string name, int? page)

And as an added bonus, it also includes a CacheFilter.

作为额外的奖励,它还包括一个 CacheFilter。

回答by EKS

For .NET Core 2.1 there is a new package that can be used ( Microsoft.AspNetCore.ResponseCompression )

对于 .NET Core 2.1,有一个可以使用的新包( Microsoft.AspNetCore.ResponseCompression )

Simple sample to get going, after installing the package:

安装软件包后,开始使用的简单示例:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddResponseCompression();

        services.AddResponseCompression(options =>
        {
            options.Providers.Add<GzipCompressionProvider>();
            options.EnableForHttps = true;
        });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseResponseCompression();
    }
}

You can read more about it here: https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-2.1&tabs=aspnetcore2x

您可以在此处阅读更多相关信息:https: //docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-2.1&tabs=aspnetcore2x