.net的网站,Viewstate视图状态的隐藏内容有时候会很长,影响到搜索引擎的收录问题,下面这段代码通过重写System.Web.UI.Page中的Render方法,来实现把ViewState调整到网站的底部。
View Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using System.Web.UI; using System.Threading; using System.Text.RegularExpressions; using System.Text; ////// 重写System.Web.UI.Page里面的Render方法,实现把viewstate的位置放到网站底部 /// namespace BLL { public class ViewstateMethod : System.Web.UI.Page { #region 重写Render方法,调viewstate位置并输出 protected override void Render(HtmlTextWriter writer) { StringBuilder sb = new StringBuilder(); System.IO.TextWriter tw = new System.IO.StringWriter(sb); HtmlTextWriter OriginalStream = new HtmlTextWriter(tw); base.Render(OriginalStream); string s = sb.ToString(); Regex regex = new Regex( "\r\n \r\n", RegexOptions.IgnoreCase); Match m = regex.Match(s); if (m.Success) { s = regex.Replace(s, String.Empty); s = s.Replace("", m.Groups[0] + Environment.NewLine + ""); } writer.Write(s); } #endregion } }
如果遇到不能用的情况,中间正则的那行可作修改。
使用页面直接继承这个类即可。