曹永思

导航

ASP.NET 压缩输出的HTML字符

重写Render

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.IO;
using System.Text.RegularExpressions;

namespace RideWtritePage
{
    public class Page : System.Web.UI.Page
    {
        protected override void Render(HtmlTextWriter writer)
        {
            //把最终要输出的html压缩后再输出 曹永思-博客园
            StringWriter html = new StringWriter();
            HtmlTextWriter tw = new HtmlTextWriter(html);
            base.Render(tw);
            string outhtml = html.ToString();
            outhtml = Regex.Replace(outhtml, "\\s+", " ");
            outhtml = Regex.Replace(outhtml, ">\\s+<", "><");
            outhtml = outhtml.Trim();
            writer.Write(outhtml);
        }

    }
}

 




Aspx.cs页面后台继承重写的类
 public partial class index : RideWtritePage.Page

 

 

 

posted on 2013-11-14 15:19  曹永思  阅读(298)  评论(0编辑  收藏  举报