如何在 Asp .Net 3.5 Web 应用程序中使用 gzip 压缩来压缩 JavaScript 和 CSS?

发布于 2024-08-16 05:34:12 字数 94 浏览 3 评论 0原文

如何在 Asp .Net 3.5 Web 应用程序中使用 gzip 压缩来压缩 JavaScript 和 CSS? Gzip 压缩会导致 CSS 菜单和验证器脚本中出现错误。

How to compress JavaScript and CSS using gzip compression in Asp .Net 3.5 web application? Gzip compression results error in CSS menu and validators scripts.

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

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

发布评论

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

评论(4

淡淡绿茶香 2024-08-23 05:34:12

为什么不使用 Http压缩?这将使您能够压缩所有静态内容,包括 javascript 和 CSS 文件。

Http压缩 IIS 6

Http压缩 IIS 7

Why don't you use HttpCompression? That would enable you to compress all static content including javascript and CSS files.

HttpCompression IIS 6

HttpCompression IIS 7

苹果你个爱泡泡 2024-08-23 05:34:12

(我已经使用这种技术很长一段时间了——因此有了 vb.net 代码!)

虽然 IIS 6.0 不支持压缩,但大多数浏览器支持基本的 gzip 压缩,并且它们通过在每个浏览器中发送标头来通知服务器这种能力。要求。下面的代码展示了如何使用 System.IO.Compression 命名空间向输出流添加过滤器,该过滤器在检查和设置正确的标头的同时压缩输出。

 context.Response.Clear()
 context.Response.Buffer = True

 context.Response.AddHeader("content-disposition", String.Format( "attachment;filename={0}", fileName))
 context.Response.ContentEncoding = Encoding.UTF8

 context.Response.Cache.SetCacheability(HttpCacheability.Private)

 'Compress the output as it may be very large
 'When flushing or closing+ending the stream, the compression filter does not have a chance to write the compression footer
 'Therefore, make sure the compression filter stream is closed before flushing
 AddCompression(context)

 context.Response.ContentType = "application/vnd.ms-excel" 'This example was an excel doc

 'Write to response
 context.Response.Write(your-data-here)

 'context.Response.Flush() 'Do not flush if using compression
 'context.Response.Close()
 context.Response.End()

AddCompression 方法检查适当的标头并将压缩过滤器流添加到输出:

'Add compression to the response stream
Public Sub AddCompression(ByVal context As HttpContext)

    Dim acceptEncoding As String = context.Request.Headers("Accept-Encoding")
    If acceptEncoding Is Nothing OrElse acceptEncoding.Length = 0 Then Return

    'Convert to lower to check
    acceptEncoding = acceptEncoding.ToLower

    'Gzip or Compress compression
    'Compress compression is quicker and performs better compression so try that first
    If (acceptEncoding.Contains("deflate")) Then

        context.Response.Filter = New DeflateStream(context.Response.Filter, CompressionMode.Compress)
        context.Response.AppendHeader("Content-Encoding", "deflate")

    ElseIf acceptEncoding.Contains("gzip") Then

        context.Response.Filter = New GZipStream(context.Response.Filter, CompressionMode.Compress)
        context.Response.AppendHeader("Content-Encoding", "gzip")

    End If

End Sub

(I've been using this technique for quite a while - hence the vb.net code!)

Although compression is not supported by IIS 6.0, most browsers support basic gzip compression and they notify the server of this ability by sending a header in each request. The following piece of code shows how to use the System.IO.Compression namespace to add a filter to the output stream that compresses the output whilst checking and setting the correct headers.

 context.Response.Clear()
 context.Response.Buffer = True

 context.Response.AddHeader("content-disposition", String.Format( "attachment;filename={0}", fileName))
 context.Response.ContentEncoding = Encoding.UTF8

 context.Response.Cache.SetCacheability(HttpCacheability.Private)

 'Compress the output as it may be very large
 'When flushing or closing+ending the stream, the compression filter does not have a chance to write the compression footer
 'Therefore, make sure the compression filter stream is closed before flushing
 AddCompression(context)

 context.Response.ContentType = "application/vnd.ms-excel" 'This example was an excel doc

 'Write to response
 context.Response.Write(your-data-here)

 'context.Response.Flush() 'Do not flush if using compression
 'context.Response.Close()
 context.Response.End()

The AddCompression method checks the appropriate headers and adds a compression filter stream to the output:

'Add compression to the response stream
Public Sub AddCompression(ByVal context As HttpContext)

    Dim acceptEncoding As String = context.Request.Headers("Accept-Encoding")
    If acceptEncoding Is Nothing OrElse acceptEncoding.Length = 0 Then Return

    'Convert to lower to check
    acceptEncoding = acceptEncoding.ToLower

    'Gzip or Compress compression
    'Compress compression is quicker and performs better compression so try that first
    If (acceptEncoding.Contains("deflate")) Then

        context.Response.Filter = New DeflateStream(context.Response.Filter, CompressionMode.Compress)
        context.Response.AppendHeader("Content-Encoding", "deflate")

    ElseIf acceptEncoding.Contains("gzip") Then

        context.Response.Filter = New GZipStream(context.Response.Filter, CompressionMode.Compress)
        context.Response.AppendHeader("Content-Encoding", "gzip")

    End If

End Sub

将以下代码添加到 Global.asax 并将其放入根目录中。

<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.IO.Compression" %>

 <script runat="server">
 void Application_PreRequestHandlerExecute(object sender, EventArgs e)
 {
HttpApplication app = sender as HttpApplication;
string acceptEncoding = app.Request.Headers["Accept-Encoding"];
Stream 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 == null || acceptEncoding.Length == 0)
    return;

acceptEncoding = acceptEncoding.ToLower();

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

add the below code to Global.asax and put it into your root directory.

<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.IO.Compression" %>

 <script runat="server">
 void Application_PreRequestHandlerExecute(object sender, EventArgs e)
 {
HttpApplication app = sender as HttpApplication;
string acceptEncoding = app.Request.Headers["Accept-Encoding"];
Stream 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 == null || acceptEncoding.Length == 0)
    return;

acceptEncoding = acceptEncoding.ToLower();

if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
{
    // defalte
    app.Response.Filter = new DeflateStream(prevUncompressedStream,
        CompressionMode.Compress);
    app.Response.AppendHeader("Content-Encoding", "deflate");
} else if (acceptEncoding.Contains("gzip"))
{
    // gzip
    app.Response.Filter = new GZipStream(prevUncompressedStream,
        CompressionMode.Compress);
    app.Response.AppendHeader("Content-Encoding", "gzip");
}
}
</script>
固执像三岁 2024-08-23 05:34:12

下载 gzip 压缩示例代码并将其添加到您的 Global.asax 文件中。

Download gzip compression sample code and add it to your Global.asax file.

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