输出HTML源码进行压缩

        //APS.NET MVC 对输出HTML源码进行压缩
        ///

 
        /// 重写Render方法,来压缩输出的内容  
        ///
 
        ///  
        protected override void Render(HtmlTextWriter writer)
        {
            StringWriter html = new StringWriter();
            HtmlTextWriter tw = new HtmlTextWriter(html);
            base.Render(tw);

            string outhtml = html.ToString();
            //压缩  


            var reg = new Regex(@"\s*(]+[^>]*>)\s+(]+[^>]*>)\s*");
            outhtml = reg.Replace(outhtml, m => m.Groups[1].Value + m.Groups[2].Value);

            //移除html标签之间的空格 
            outhtml = new Regex(@"(?<=>)[\s|\n|\t]*(?=<)").Replace(outhtml, string.Empty);

            //移除多余空格和换行符
            outhtml = new Regex(@"\n+\s+").Replace(outhtml, string.Empty);

            //outhtml = Regex.Replace(outhtml, @"(?<=>)\s|\n|\t(?=<)", string.Empty);
            outhtml = outhtml.Trim();

            //输出到客户端  
            writer.Write(outhtml);
        }  

你可能感兴趣的:(程序)