Use Microsoft ASP. NET WebForm to compress JS and CSS in ASP. WEB optimization

Source: Internet
Author: User

Use static resource compression to consolidate static resource files reduce the number of client requests, compress file sizes, and reduce network traffic loss

Need to use Microsoft ASP. NET Web Optimization, installation method:

Install-package Microsoft.AspNet.Web.Optimization

Refer to the MVC architecture method, add BundleConfig.cs in App_start

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Optimization;namespaceemptywebform{ Public classBundleconfig { Public Static voidregisterbundles (bundlecollection bundles) {bundles. ADD (NewScriptbundle ("~/bundles/jquery"). Include ("~/scripts/jquery-{version}.js")); Bundles. ADD (NewScriptbundle ("~/bundles/jqueryui"). Include ("~/scripts/jquery-ui-{version}.js")); Bundles. ADD (NewScriptbundle ("~/bundles/jqueryval"). Include ("~/scripts/jquery.unobtrusive*",                        "~/scripts/jquery.validate*")); Bundles. ADD (NewScriptbundle ("~/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 (NewScriptbundle ("~/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 (NewScriptbundle ("~/bundles/modernizr"). Include ("~/scripts/modernizr-*")); Bundles. ADD (NewStylebundle ("~/bundles/themes/base/style"). Include ("~/content/themes/base/style*")); }    }}

Add Style1.css and Style2.css as tests under themes

1{2border:solid 1px red3 }
Style1.css
1{2color:red3 }
Style2.css

Modify the page to apply CSS and JS method as follows:

<%@ Page Language="C #"AutoEventWireup="true"codebehind="Index.aspx.cs"Inherits="Emptywebform.index" %><!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Headrunat= "Server"><Metahttp-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>    <formID= "Form1"runat= "Server">    <Div>        </Div>    </form></Body></HTML>

In the results of the page request, the compressed files are automatically merged according to the configuration, as detailed below:

The CSS compression effect is as follows, two style sheet files are merged into one and compressed

Reference:

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

Use Microsoft ASP. NET WebForm to compress JS and CSS in ASP. WEB optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.