c# - 如何压缩数据

标签 c# asp.net iis-7 compression gzip

1. 我的主机使用 IIS 7,IIS 无权访问设置。 现在 web.config 或其他我发送了一个 js/css/aspx 来应用 Gzip。

2.如何为iis6启用etags

如何为我的网站启用此功能 这行不通(Yslow)

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
            <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
            <dynamicTypes>
                <add mimeType="text/*" enabled="true"/>
                <add mimeType="message/*" enabled="true"/>
                <add mimeType="application/javascript" enabled="true"/>
                <add mimeType="*/*" enabled="false"/>
            </dynamicTypes>
            <staticTypes>
                <add mimeType="text/*" enabled="true"/>
                <add mimeType="message/*" enabled="true"/>
                <add mimeType="application/javascript" enabled="true"/>
                <add mimeType="*/*" enabled="false"/>
            </staticTypes>
        </httpCompression>
        <urlCompression doStaticCompression="true" doDynamicCompression="true"/>

编辑

gloabal.asax 中的这段代码很好用,但 Yslow 仍然显示不使用 Gzip??

void Application_PreRequestHandlerExecute(对象发送者,EventArgs e) { HttpApplication app = sender as HttpApplication; string acceptEncoding = app.Request.Headers["Accept-Encoding"]; 流 prevUncompressedStream = app.Response.Filter;

    if (!(app.Context.CurrentHandler is Page ||
        app.Context.CurrentHandler.GetType().Name == "SyncSessionlessHandler") ||
        app.Request["HTTP_X_MICROSOFTAJAX"] != null)
        return;

    if (acceptEncoding.Contains("gzip") )
        return;

    acceptEncoding = acceptEncoding.ToLower();

    if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
    {
        // gzip
        app.Response.Filter = new GZipStream(prevUncompressedStream,
            CompressionMode.Compress);
        app.Response.AppendHeader("Content-Encoding", "gzip");


    }
    else if (acceptEncoding == null || acceptEncoding.Length == 0)
    {
        // defalte
        app.Response.Filter = new DeflateStream(prevUncompressedStream,
            CompressionMode.Compress);
        app.Response.AppendHeader("Content-Encoding", "deflate");

    }
}

Article Link

最佳答案

我认为在代码中实现压缩是错误的方法;我了解您的情况,我相信这很困难,但我会考虑换个房东。

this code in gloabal.asax good work but still Yslow show does not use Gzip??

你的意思是当你在本地测试时它可以工作,但当它部署到主机上时却不行?我肯定已经看到一些代理(例如公司网关)从网络中删除了“accept-encoding gzip”,所以这可能是需要考虑的事情。

关于c# - 如何压缩数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6863043/

相关文章:

c# - 将错误代码大量转换为异常

c# - 同步两个枚举

c# - Access-Control-Allow-Headers 不允许请求 header 字段 x-user-session

asp.net - 如何每年更新数据库字段

c# - 文本转语音可以在 Windows Azure 上运行吗?

authentication - IIS7 : Setup Integrated Windows Authentication like in IIS6

php - 从 PHP 使用 DOTNET 时遇到问题。

c# - 使 PInvoked 方法保持事件状态

c# - 我想从数据库中检索图像并根据用户需要裁剪它

iis-7 - 创建全局 IIS 7 重写规则