ViewState的喜和悲。帮忙!

wpj2101928 2009-03-07 03:57:32
在asp.net页面中经常会出现一些ViewState的html标记,也许某些时候你会禁用ViewState,但是某些情况下你不得不使用它——因为它的便捷性,但是由于在默认情况下,ViewState的HTML标记总是在页面的最前面,而且都是一些没有意义的内容,一般的搜索引擎收录的时候 就会将这些无意义的字符串收录进去,这样就会严重影响你所制作的网页在搜索引擎的排名。有没有解决办法?答案是有的,可以将ViewState的Html标记移到底部,不影响性能,对搜索引擎更友好。这种方法就是重写页面的Render,将ViewState的Html标记移到底部。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Text;


public partial class _Default : System.Web.UI.Page
{
//ViewState的Html标记的正则表达式
private static readonly Regex viewStateRegex = new Regex(@"(<input type=""hidden"" name=""__VIEWSTATE"" id=""__VIEWSTATE"" value=""[w+\/=]+"" />)", RegexOptions.Multiline | RegexOptions.Compiled);
//</form>标记的正则表达式
private static readonly Regex endFormRegex = new Regex(@"</form>", RegexOptions.Multiline | RegexOptions.Compiled);

protected override void Render(HtmlTextWriter writer)
{
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlWriter);

string html = stringWriter.ToString();
Match viewStateMatch = viewStateRegex.Match(html);
string viewStateString = viewStateMatch.Captures[0].Value;//找出ViewState的Html标记
html = html.Remove(viewStateMatch.Index, viewStateMatch.Length);//替换掉ViewState的html标记

Match endFormMath = endFormRegex.Match(html, viewStateMatch.Index);
html = html.Insert(endFormMath.Index, viewStateString);//将ViewState的Html标记插入到</form>标记之前
writer.Write(html);

}

protected void Page_Load(object sender, EventArgs e)
{

}
}


现在出现了一个问题:页面加载就出现这样的问题。不知道怎么解决,
string viewStateString = viewStateMatch.Captures[0].Value;//找出ViewState的Html标记
指定的参数已超出有效值的范围。
参数名: i

所以请大家能帮忙下!万分感谢
...全文
184 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouliyiyi 2010-10-28
  • 打赏
  • 举报
回复
当页面有gridview分页或排序的时候,viewstate的内容移动form结尾处点分页会出错的。
KENLIMYTH 2009-03-20
  • 打赏
  • 举报
回复
UP
wpj2101928 2009-03-14
  • 打赏
  • 举报
回复
sp 我还是不知道怎么写啊。不是":XVPage "吗。怎么没有效果啊
Teng_s2000 2009-03-13
  • 打赏
  • 举报
回复
sp大哥就是牛啊

不过lz至管使用Microsoft的东西,就得受他的气了,不合理的地方多了啊
  • 打赏
  • 举报
回复
删除你的 protected override void Render(HtmlTextWriter writer),你可以把我这个类型取代你的 System.Web.UI.Page 作为父类试试。
abstract public class XVPage : Page
{
static private DirectoryInfo _Dir;

private DirectoryInfo Dir
{
get
{
if (_Dir == null)
{
_Dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data"));
if (!_Dir.Exists)
_Dir.Create();
_Dir = new DirectoryInfo(Path.Combine(_Dir.FullName, "ViewState"));
if (!_Dir.Exists)
_Dir.Create();
}
return _Dir;
}
}

protected override object LoadPageStateFromPersistenceMedium()
{
PageStatePersister ps = this.PageStatePersister;
ps.Load();
if (ps.ControlState != null)
ps.ControlState = 反序列化对象((string)ps.ControlState);
if (ps.ViewState != null)
ps.ViewState = 反序列化对象((string)ps.ViewState);
return new Pair(ps.ControlState, ps.ViewState);
}

protected override void SavePageStateToPersistenceMedium(object state)
{
PageStatePersister ps = this.PageStatePersister;
if (state is Pair)
{
Pair pair = (Pair)state;
ps.ControlState = pair.First;
ps.ViewState = pair.Second;
}
else
{
ps.ViewState = state;
}
if (ps.ControlState != null)
ps.ControlState = 序列化对象(ps.ControlState);
if (ps.ViewState != null)
ps.ViewState = 序列化对象(ps.ViewState);
ps.Save();
}

private object 反序列化对象(string stateID)
{
string stateStr = (string)Cache[stateID];
string file = Path.Combine(Dir.FullName, stateID);
if (stateStr == null)
stateStr = File.ReadAllText(file);
else
Cache.Remove(stateID);
return new ObjectStateFormatter().Deserialize(stateStr);
}

private string 序列化对象(object obj)
{
string value = new ObjectStateFormatter().Serialize(obj);
string stateID = (DateTime.Now.Ticks + (long)value.GetHashCode()).ToString(); //产生离散的id号码
File.WriteAllText(Path.Combine(Dir.FullName, stateID), value);
Cache.Insert(stateID, value);
return stateID;
}

protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
DateTime dt = DateTime.Now.AddHours(-2);
foreach (FileInfo fl in Dir.GetFiles())
if (fl.LastAccessTime < dt)
try
{
fl.Delete();
}
catch
{
}
}
}
wpj2101928 2009-03-07
  • 打赏
  • 举报
回复
怎么没人啊!

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧