在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所以请大家能帮忙下!万分感谢

解决方案 »

  1.   

    删除你的 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
                    {
                    }
        }
    }
      

  2.   

    sp大哥就是牛啊不过lz至管使用Microsoft的东西,就得受他的气了,不合理的地方多了啊
      

  3.   

    sp  我还是不知道怎么写啊。不是":XVPage "吗。怎么没有效果啊
      

  4.   

    当页面有gridview分页或排序的时候,viewstate的内容移动form结尾处点分页会出错的。