asp.net页面过滤所有换行符和多余空格
2014-03-31 17:08
267 查看
不知道大家注意到了没有,Google和Baidu网页的HTML源代码是混合在一起的。HTML代码混合在一起,出发点是为了减小网页体积,从而加快网页加载速度。
写个函数把网页HTML源代码的换行符和空格过滤掉其实并不难,我这里是写了个基类,在asp.net编程时,页面只要继承这个基类,那么输出的HTML代码就会自动去掉换行符,和多余的空格符号,例如“> <”之间的空格符号。
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.Text.RegularExpressions;
usingSystem.IO;
/// <summary>
/// PageBase 页面基类
/// </summary>
publicclassPageBase:System.Web.UI.Page
{
protectedoverridevoidRender(HtmlTextWriter writer)
{
StringWriter sw =newStringWriter();
HtmlTextWriter htmlWriter =newHtmlTextWriter(sw);
base.Render(htmlWriter);
string html = sw.ToString();
html =Regex.Replace(html,"[\f\n\r\t\v]","");
html =Regex.Replace(html," {2,}"," ");
html =Regex.Replace(html,">[ ]{1}",">");
writer.Write(html);
}
}
写个函数把网页HTML源代码的换行符和空格过滤掉其实并不难,我这里是写了个基类,在asp.net编程时,页面只要继承这个基类,那么输出的HTML代码就会自动去掉换行符,和多余的空格符号,例如“> <”之间的空格符号。
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.Text.RegularExpressions;
usingSystem.IO;
/// <summary>
/// PageBase 页面基类
/// </summary>
publicclassPageBase:System.Web.UI.Page
{
protectedoverridevoidRender(HtmlTextWriter writer)
{
StringWriter sw =newStringWriter();
HtmlTextWriter htmlWriter =newHtmlTextWriter(sw);
base.Render(htmlWriter);
string html = sw.ToString();
html =Regex.Replace(html,"[\f\n\r\t\v]","");
html =Regex.Replace(html," {2,}"," ");
html =Regex.Replace(html,">[ ]{1}",">");
writer.Write(html);
}
}
相关文章推荐
- asp.net页面过滤所有换行符和多余空格
- asp.net页面过滤所有换行符和多余空格
- asp.net页面过滤所有换行符和多余空格
- asp.net页面过滤所有换行符和多余空格
- c#过滤所有换行符和多余空格
- 过滤ASP.NET输出HTML中的无用空格
- ASP.NET - 截取固定长度字符串显示在页面,多余部分显示为省略号
- asp.net采集页面上所有图像图片资源的具体方法
- ASP.NET正则表达式提取网页网址、标题、图片实例以及过滤所有HTML标签实例
- asp.net中去除字符串中的所有空格字符
- Asp.net清空页面上的所有TextBox
- 用asp.net实现 取页面中的所有链接
- [导入]如何在ASP.NET下遍历指定页面上所有控件
- asp.net过滤HTML标签,只保留换行与空格
- ASP.net在页面所有内容生成后、输出内容前对页面内容进行操作
- 递归输出ASP.NET页面所有控件的类型和ID的代码
- asp.net 正则表达式过滤所有html标签
- Asp.Net : 捕捉和记录网站中出现的所有未处理错误,抛出详细的页面来源和访问ip,调用的接口方法及异常实例(记事本日志,系统日志及数据库日志)
- ASP.NET MVC中给所有的cshtml页面引用命名空间
- c#扩展方法奇思妙用 ASP.NET MVC 篇:巧用扩展方法优先级,美化所有页面 TextBoxFor 文本框