asp.net-mvc 在 IIS7 上使用 MVC3 时如何启用 gzip 压缩?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6992524/
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 01:23:54  来源:igfitidea点击:

How do I enable gzip compression when using MVC3 on IIS7?

asp.net-mvcasp.net-mvc-3iiscompressiongzip

提问by imarkic84

Does anybody know how to enable gzip compression in MVC 3? I'm using IIS7.

有人知道如何在 MVC 3 中启用 gzip 压缩吗?我正在使用 IIS7。

Google Chrome Audit's result:

谷歌浏览器审计结果:

  1. Enable gzip compression (4)
  2. Compressing the following resources with gzip could reduce their transfer size by about two thirds (~92.23KB):
  3. /mydomain/ could save ~1.53KB
  4. jquery-1.4.4.min.js could save ~51.35KB
  5. Cufon.js could save ~11.89KB
  6. Futura.js could save ~27.46KB
  1. 启用 gzip 压缩 (4)
  2. 使用 gzip 压缩以下资源可以将其传输大小减少约三分之二 (~92.23KB):
  3. /mydomain/ 可以节省 ~1.53KB
  4. jquery-1.4.4.min.js 可以节省 ~51.35KB
  5. Cufon.js 可以节省 ~11.89KB
  6. Futura.js 可以节省 ~27.46KB

回答by Rob

You can configure compression through your web.configfile as follows:

您可以通过您的web.config文件配置压缩如下:

<system.webServer>
    <urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>

You can find documentation of this configuration element at iis.net/ConfigReference. This is the equivalent of:

您可以在iis.net/ConfigReference找到此配置元素的文档。这相当于:

  1. Opening Internet Information Services (IIS Manager)
  2. Navigating through the tree-view on the left until you reach the virtual directory you wish to modify
  3. Selecting the appropriate virtual directory so that the title of the right-hand pane becomes the name of said virtual directory.
  4. Choosing "Compression" under "IIS" in the right-hand pane
  5. Ticking both options and choosing "Apply" under "Actions" on the far right.
  1. 打开 Internet 信息服务(IIS 管理器)
  2. 在左侧的树视图中导航,直到到达要修改的虚拟目录
  3. 选择适当的虚拟目录,使右侧窗格的标题成为所述虚拟目录的名称。
  4. 在右侧窗格中的“IIS”下选择“压缩”
  5. 勾选两个选项并在最右侧的“操作”下选择“应用”。

Note: (As pointed out in the comments)You need to ensure that Http Dynamic Compression is installed otherwise setting doDynamicCompression="true"will not have any effect. The quickest way to do this is:

注意:(如评论中所指出)您需要确保安装了 Http Dynamic Compression,否则设置doDynamicCompression="true"将不起作用。最快的方法是:

  1. Start > Type optionalfeatures(this is the quickest way to get to the "Turn Windows Features on or off" window)
  2. Navigate to Internet Information Services > World Wide Web Services > Performance Features in the "Windows Features" treeview
  3. Ensure "Dynamic Content Compression" is ticked
  4. Click "Ok" and wait whilst Windows installs the component
  1. 开始 > 类型optionalfeatures(这是进入“打开或关闭 Windows 功能”窗口的最快方式)
  2. 在“Windows 功能”树视图中导航到 Internet 信息服务 > 万维网服务 > 性能功能
  3. 确保勾选“动态内容压缩”
  4. 单击“确定”并等待 Windows 安装组件

回答by NetProvoke

You could do this in code if you rather do that. I would make a basecontroller which every control inherits from and decorate it with this attribute below.

如果你愿意,你可以在代码中做到这一点。我会制作一个基本控制器,每个控件都继承自它,并用下面的这个属性装饰它。

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

回答by nakhli

Compression is enabled/disabled at the server's level. See IIS compression module in iis management console.

在服务器级别启用/禁用压缩。参见 iis 管理控制台中的 IIS 压缩模块。

Here are the instructions for IISfrom microsoft site.

以下是来自微软网站的IIS 说明