asp.net webform 中使用Microsoft ASP.NET Web Optimization壓縮js及css

來源:互聯網
上載者:User

標籤:

使用靜態資源壓縮可以合并靜態資源檔案減少用戶端請求數量,壓縮檔大小,減少網路流量的損耗

需要用到Microsoft ASP.NET Web Optimization,安裝方法:

Install-Package Microsoft.AspNet.Web.Optimization

 

參考MVC構架的方法,App_Start中添加BundleConfig.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Optimization;namespace EmptyWebForm{    public class BundleConfig    {        public static void RegisterBundles(BundleCollection bundles)        {            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(                        "~/Scripts/jquery-{version}.js"));            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(                        "~/Scripts/jquery-ui-{version}.js"));            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(                        "~/Scripts/jquery.unobtrusive*",                        "~/Scripts/jquery.validate*"));                      bundles.Add(new ScriptBundle("~/bundles/WebFormsJs").Include(                  "~/Scripts/WebForms/WebForms.js",                  "~/Scripts/WebForms/WebUIValidation.js",                  "~/Scripts/WebForms/MenuStandards.js",                  "~/Scripts/WebForms/Focus.js",                  "~/Scripts/WebForms/GridView.js",                  "~/Scripts/WebForms/DetailsView.js",                  "~/Scripts/WebForms/TreeView.js",                  "~/Scripts/WebForms/WebParts.js"));            bundles.Add(new ScriptBundle("~/bundles/MsAjaxJs").Include(                "~/Scripts/WebForms/MsAjax/MicrosoftAjax.js",                "~/Scripts/WebForms/MsAjax/MicrosoftAjaxApplicationServices.js",                "~/Scripts/WebForms/MsAjax/MicrosoftAjaxTimer.js",                "~/Scripts/WebForms/MsAjax/MicrosoftAjaxWebForms.js"));            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(                "~/Scripts/modernizr-*"));            bundles.Add(new StyleBundle("~/bundles/themes/base/style").Include("~/Content/themes/base/style*"));                     }    }}

在themes下添加style1.css與style2.css作為測試

1 body {2 border:solid 1px red3 }
style1.css
1 input {2 color:red3 }
style2.css

修改頁面應用css及js方法如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="EmptyWebForm.Index" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title>    <%:System.Web.Optimization.Scripts.Render("~/bundles/jquery") %>    <%:System.Web.Optimization.Scripts.Render("~/bundles/WebFormsJs")%>    <%:System.Web.Optimization.Styles.Render("~/bundles/themes/base/style")%>    </head><body>    <form id="form1" runat="server">    <div>        </div>    </form></body></html>

頁面請求結果中,根據配置會自動合并壓縮檔,詳細如下:

css壓縮效果如下,兩個樣式表檔案被合并為一個並且進行了壓縮

 

參考:

https://blogs.msdn.microsoft.com/rickandy/2012/08/14/adding-bundling-and-minification-to-web-forms/

asp.net webform 中使用Microsoft ASP.NET Web Optimization壓縮js及css

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.