调整viewState的位置,有助于SEO

Asp.net 中的viewState默认出现在Form内的第一个元素,如果viewState很大的话会影响搜索引擎搜索页面,我们可以通过重写render来调整其位置到form最后,具体代码如下:

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(
                "<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"/(.*?)\" />",
                RegexOptions.IgnoreCase);
        Match m = regex.Match(s);
        if(m.Success)
        {
            s = regex.Replace(s, String.Empty);
            s = s.Replace("</form>", m.Groups[0] + "</form>");
        }
        writer.Write(s);
    }

 

 

 

 

posted @ 2010-06-03 15:29  Flymouse  阅读(289)  评论(0编辑  收藏  举报