Compress ASPX pages two ways to remove extra space

Source: Internet
Author: User
Tags datetime

  This article mainly introduces two ways to compress ASPX pages to remove extra space, you can compress aspx before publishing the page, without wasting the CPU on the Web server, you need friends to refer to the following

Two ways to implement:  1 read ASPX files on one line and then process   2 read the ASPX file once and then handle   processing logic:  Replace " " as "" (replace two spaces with one space), Replace all line breaks with empty characters (limit compression)   Considerations:  1) A line to handle the need for additional processing of service-side control wrapping in case of extreme compression, such as the     code as follows: Line 1:<asp:label  runat= "Server"   line 2:id= "LB1"   ....  lines 3:.../>     This situation can cause problems in one line,     2 In addition JS script Inline single note   recommendation use "/**/" instead of "//"   Use results:    A line of processing is slightly faster than one-time processing, for two hundred or three hundred of the ASPX file, the gap in the millisecond level. But for the whole project to deal with, as the number of documents increased, the gap should be reflected.   One-time read processing can be done without limiting compression, so that the problems with server controls and inline Single-line annotations are not considered.   I usually rarely use inline and single-line comments and server controls so the compression effect is very obvious, generally 500-600 lines of source code compression after less than 50 lines, size reduction of about one-third.   But this compression effect may have much to do with whether you use a server-side data listing control and how to use it, and I generally use repeater only.     Code as follows: public static string Replace (String source,string oldstr,string newstr)        {&N Bsp          int count = regex.matches (source, oldstr). Count;            for (int i = 0; i < count; i++)            {&NB Sp              source = source. Replace (Oldstr, NEWSTR);           &NBSP,            return source;       &NBSP          ///<summary>        ///compressing the text of the specified path Pieces of blank string and line breaks        ///compression description        ///1) Take file.readalllines read all rows of each row do process work   &N Bsp    ///2 server controls are best written on a single line, with only the tail tag and runat= "Server" Cross row processing, start tag cross behavior processing        ///3 file cannot have single-line comment "//" & nbsp      ///4 replaces line breaks and spaces        ///</summary>        ///<PA Ram Name= "FilePath" > File path </param>        public static void Compresslinebyline (String FilePath)        {           IF (! File.exists (FilePath))            {               C ONsole. WriteLine ("File does not exist, check path {0}", FilePath);                return;           &NBSP,            var start = DateTime.Now;            console.writeline ("compressing the file: {0}rn starts at {1} ...", Filepath,start.) ToString ());            var lines = File.ReadAllLines (FilePath, encoding.getencoding ("GB2312"));            for (int i = 0; i < lines. Length; i++)            {               var item = lines[i] . Trim ();                IF (item. IndexOf ("runat=" Server "") >-1)                    item + = "";                item = Item. Replace ("RN", "");                item = Replace (item, "  "," ");                lines[i] = Item;           &NBSP,            file.writealltext (FilePath, String. Join ("", Lines), encoding.getencoding ("GB2312"));            var end = DateTime.Now;            console.writeline ("ending with {0} ...", end.) ToString ());            console.writeline ("= = time consuming ====rn{0}rn", end-start);       &NBSP          ///<summary>        ///compressing the text of the specified path Blank string and line break        ///compression description        ///1 Read all text replace swap line characters and blank        ///2 There is no need to deal with the problem of server control wrapping        ///3) compression is not thorough, the end of a element and the B start tag may still have a space between        / </summary>        ///<param name= "FilePath" ></param>       &NBSP ;p ublic Static void Compressatonetime (String filePath)        {           var start = DateTime.Now;            console.writeline ("compressing the file: {0}rn starts at {1} ...", FilePath, start.) ToString ());            var lines = File.readalltext (FilePath);            file.writealltext (FilePath, replace (replace (lines, RN, ""), " ", ""), ENC Oding. GetEncoding ("GB2312"));            var end = DateTime.Now;            console.writeline ("ending with {0} ...", end.) ToString ());            console.writeline ("= = time consuming ====rn{0}rn", end-start);        }    

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.