如何在 asp.net MVC 中压缩内容?

发布于 2024-09-25 01:12:39 字数 35 浏览 0 评论 0原文

如何压缩由 ASP.NET MVC 应用程序发送的输出?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山色无中 2024-10-02 01:12:39

这是我使用的(截至目前):

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);
        }
    }
}

控制器中的使用:

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

还有其他变体,但这效果很好。 (顺便说一句,我在 BaseController 上使用 [压缩] 属性来保存整个项目的重复,而上面是在逐个控制器的基础上进行的。

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

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

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.

[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]
public class BaseController : Controller
{...}
坦然微笑 2024-10-02 01:12:39

看看 这篇文章 概述了利用操作过滤器的巧妙方法。

例如:

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

作为一个额外的好处,它还包括一个 CacheFilter。

Have a look at this article which 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.

青衫负雪 2024-10-02 01:12:39

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

安装该包后,可以使用简单的示例:

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();
    }
}

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

For .NET Core 2.1 there is a new package that can be used ( 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://learn.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-2.1&tabs=aspnetcore2x

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文