【输出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+(?[^\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);
}
推荐阅读
- 关于欧几里得算法和拓展欧几里得算法
- 蛇精,蜈蚣
- 程序|android 不销毁当前activity,返回上一层,亲测有效
- c语言while循环打印等腰三角型
- 电子DIY|《电子DIY》之《单片机实践项目》之基于51单片机的TCS230颜色识别模块
- JsonHelper
- Jmysy-Pear-Admin-Layui
- 获取游览器类型
- 框架|Java如何使用Web3j开发以太坊区块链Dapp的教程