SoFunction
Updated on 2025-03-07

Two ways to compress aspx page to delete extra spaces

Two methods are implemented:
1) Read aspx files one by one and then process them
2) Read the aspx file at one time and then process it
Processing logic:
Replace "  " to " (replace two spaces with one space), replace all newline characters with empty characters (extreme compression)
Notes:
1) One line by line to handle the situation where the server control line breaks is required in the case of extreme compression, such as

Copy the codeThe code is as follows:

Line 1:<asp:Label  runat="server"
Line 2: ID="lb1"   ....
Line 3:.../> 

In this case, there will be problems when dealing with one line by one

2) In addition, single-line comments inlined in JS scripts
Recommended to use "/**/" instead of "//"
Use results:

The processing of one line is slightly faster than the one-time processing. For two or three hundred lines of aspx files, the gap is at the millisecond level. However, as the number of files increases, the gap should be reflected in the entire project.
One-time read processing can be done without extreme compression, so that the problems of server controls and inline single-line comments can be ignored.
I usually rarely use inline comments, single-line comments and server controls, so the compression effect is very obvious. Generally, the source code of 500-600 lines is less than 50 lines after compression, and the size is reduced by about one-third.
However, this compression effect may have a lot to do with whether you use the server data list control and how to use it. I usually only use repeater.

Copy the codeThe code is as follows:

public static String Replace(String source,String oldStr,String newStr)
       {
           int count = (source, oldStr).Count;
           for (int i = 0; i < count; i++)
           {
               source = (oldStr, newStr);
           }
           return source;
       }

       /// <summary>
/// Compress the file blank string and line breaks of the specified path
/// Compression instructions
/// 1) Read all rows and each row for processing
/// 2) It is best to write the server control in one line, and only process the tail label and runat="server" across rows, and start the tag cross-behavior processing.
/// 3) The file cannot have a single line comment "//"
/// 4) Replace newlines and spaces
       /// </summary>
/// <param name="filePath">File Path</param>
       public static void CompressLineByLine(String filePath)
       {
           if (!(filePath))
           {
("The file does not exist, check the path {0}", filePath);
               return;
           }
           var start = ;
("Compressing file: {0}\r\nStarting at {1}...",
filePath,());
           var lines = (filePath,
("GB2312"));
           for (int i = 0; i < ; i++)
           {
               var item = lines[i].Trim();
               if (("runat=\"server\"") > -1)
                   item += " ";
               item = ("\r\n", "");
               item = Replace(item, "  ", " ");
               lines[i] = item;
           }
           (filePath, ("", lines),
("GB2312"));
           var end = ;
("End at {0}...", ());
("====Time-consuming====\r\n{0}\r\n", end - start);
       }

       /// <summary>
/// Compress the file blank string and line breaks of the specified path
/// Compression instructions
/// 1) Read out all text at once and replace line breaks and blanks
/// 2) No need to deal with the problem of server control line wrapping
/// 3) The compression is not thorough, there may still be a space between the end label of element A and the start label of B.
       /// </summary>
       /// <param name="filePath"></param>
       public static void CompressAtOneTime(String filePath)
       {
           var start = ;
("Compressing file: {0}\r\nStarting at {1}...", filePath,
());
           var lines = (filePath);
           (filePath, Replace(Replace(lines, "\r\n",
""),"  "," "), ("GB2312"));
           var end = ;
("End at {0}...", ());
("====Time-consuming====\r\n{0}\r\n", end - start);
       }