在 Asp.Net WebForm 中压缩 JavaScript 和 CSS

发布于 2024-12-08 15:00:19 字数 1542 浏览 0 评论 0原文

我使用 C# Asp.net 4, 我在 Global.asax 中有这个脚本。我可以对所有 asp.net 页面进行 gzip 压缩,但不能对 JavaScript 和 CSS 进行压缩。 我想知道是否可以直接在我的 Web 应用程序中压缩 CSS 和 JS,或者仅在 IIS 中压缩。如果可能的话请指出我的代码中遗漏的内容。 谢谢

protected 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 == "*")
        if (acceptEncoding.Contains("gzip") || acceptEncoding == "*")
        {
            // gzip
            app.Response.Filter = new GZipStream(prevUncompressedStream,
                CompressionMode.Compress);
            app.Response.AppendHeader("Content-Encoding", "gzip");

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

I use C# Asp.net 4,
I have this script in Global.asax. I'm able to gzip all asp.net pages but NOT JavaScripts and CSS.
I would like to know if is possible have compressed CSS and JS directly in my web app or it possible just in IIS. If is possible please point me out what I miss in my code.
Thanks

protected 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 == "*")
        if (acceptEncoding.Contains("gzip") || acceptEncoding == "*")
        {
            // gzip
            app.Response.Filter = new GZipStream(prevUncompressedStream,
                CompressionMode.Compress);
            app.Response.AppendHeader("Content-Encoding", "gzip");

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

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

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

发布评论

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

评论(4

浅唱ヾ落雨殇 2024-12-15 15:00:20

您可以而且应该设置 IIS 来压缩静态文件。让 IIS 在它到达您的应用程序之前处理它。

您可以在 IIS 管理控制台中手动设置此项,或使用 web.config 中的 设置。

You can and should set up IIS to compress static files. Let IIS handle it before it even gets to your app.

You can set this manually in IIS Management Console or use the <httpCompression> setting in your web.config.

羁绊已千年 2024-12-15 15:00:20

是的,我们可以利用 IIS 7.0 压缩功能。另外,您确实关心性能,在应用程序中创建一种 JS 和一种 CSS 总是更好,如果不可能,最好将这些 JS 和 CCS 合并为一种 Js 和 CSS。并且一直向我们推荐AJAX Minifier。

http://www.mindfiresolutions.com /如何合并 ASPNET-1221.php 中的 Javascript 文件

http://www.mindfiresolutions.com/How-to -merge-CSS-files-in-ASPNET-1230.php

如果您需要进一步的帮助,请随时与我联系。

谢谢,
希曼舒·谢卡尔·萨胡
www.mindfiresolutions.com

Yes, we can take the advantage of IIS 7.0 compression fearutres. Also you are really concerned about performance, it is always better to create one JS and one CSS in your applications, if this is not possible, it is better to merge those JS and CCS into one Js and CSS. And also it is always recommended to us AJAX Minifier.

http://www.mindfiresolutions.com/How-to-merge-your-Javascript-files-in-ASPNET-1221.php

http://www.mindfiresolutions.com/How-to-merge-CSS-files-in-ASPNET-1230.php

Please feel free to contact me if you need further help in this.

Thanks,
Himanshu Shekhar Sahu
www.mindfiresolutions.com

吾家有女初长成 2024-12-15 15:00:19

您确定 *.css 和 *.js 由 ASP.NET 运行时处理吗?它们不是默认的,例如,您必须在 web.config 中将 runAllManagedModulesForAllRequests 设置为 true

另外,为什么不依赖 IIS 内置的压缩​​呢?

Are you sure that *.css and *.js are handled by the ASP.NET runtime? They are not by default, you'd for example have to set the runAllManagedModulesForAllRequests to true in web.config.

Also, why don't you rely on the IIS built-in compression?

谁的新欢旧爱 2024-12-15 15:00:19

提前压缩和压缩你的 CSS 和 JS,同时将所有 JS 合并到一个文件中,将所有 CSS 合并到一个文件中:

使用 YUI 压缩机进行在线 JavaScript/CSS 压缩

如果您稍微搜索一下,您会发现各种方法可以将压缩和缩小自动化到构建后过程或 CI 构建过程中。

Minify and gzip your CSS and JS ahead of time, also combine all the JS in one file and all CSS in one file:

Online JavaScript/CSS Compression Using YUI Compressor

If you search a little you will find all kinds of way to automate the compression and minification into a post-build process or CI build process.

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