asp. net output rewrite compressed page file instance code

  • 2020-11-30 08:14:00
  • OfStack

example


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.htmlControls;
using System.Text.RegularExpressions;
using System.IO;
/// <summary>
/// PageBase  The page base class 
/// </summary>
public class PageBase : System.Web.UI.Page
{
    protected override void Render(htmlTextWriter writer)
    {
        StringWriter sw = new StringWriter();
        HtmlTextWriter htmlWriter = new htmlTextWriter(sw);
        base.Render(htmlWriter);
        string html = sw.ToString();
        html = Regex.Replace(html,  " [f v] " ,  "" );
        html = Regex.Replace(html,  "  {2,} " ,  "   " );
        html = Regex.Replace(html,  " >[ ]{1} " ,  " > " );
        writer.Write(html);
    }
}

Add several.net compression modules

1. WebResourceCompression compression module

This compression module is dedicated to real-time compression ASP NET2. 0 *. Page references axd resources, 1 kind aspx page USES such as anthem net ajax framework or asp net validation controls produces axd document reference, this file is actually a js script, enable the compression module, all axd resources will be GZIP compressed and then sent to the client, This module is especially suitable for aspx pages that apply the ajax framework or refer to a large axd resource file!

Use: Unzip WebResourceCompression.dll and place it in the BIN directory of the website project and in Web.config < httpModules > Add the following statements in the configuration section:
The code is as follows:


<add name="WebResourceCompression" type="WebResourceCompression.WebResourceCompressionModule"/>

Pros: Easy to use
Disadvantages: support ASP.NET2.0 or above only, cannot compress other resources than axd!

2. PageCompression compression module


Different from the previous module, this module is specially used to compress aspx page. After enabling this compression module, aspx page will be compressed in real time. The page of 1 general 100K can be compressed to about 25K.

Use: Unzip Compression.PageCompressionModule.dll and place it in the BIN directory of the site project and in Web.config < httpModules > Add the following statements in the configuration section:
The code is as follows:


<add name="PageCompressionModule" type="Compression.PageCompressionModule,Compression.PageCompressionModule"/>

Pros: Easy to use
Disadvantages: support ASP.es72EN2.0 or above only, no resources other than aspx pages can be compressed, and axd resources referenced by pages will be invalid if compression is enabled (BUG)!

3. HttpCompress6.0 compression module


This is a support asp. net1. 2.0/0/1.1 (below 2.0 version does not support gzip compression, only supports deflate compression), can be compressed aspx all types of page requests (MimeTypes) resources, including images, script, axd, aspx js page, css files, etc., and can in web. config detailed custom to compress the types of resources and without compression the types of resources, can also be defined to compress the specified page or without compression specified page, There are also powerful customization features such as compression ratio setting (high|normal|low), but the most serious problem with this control is that enabling compression under asp.net2.0 invalidates the axd file, thus rendering ajax invalid if the page applies the ajax framework.

Usage: omitted (similar to the CompressionModule compression module recommended below)
Advantages: open source, powerful custom functions, support asp. net1. Version 2.0/0/1.1, can compress a variety of resources!
Disadvantages: Complex use, asp. net2.0 enable compression will cause axd file invalid!


Related articles: