现在的位置: 首页 > 综合 > 正文

调整ViewState的位置,让你的asp.net页面对搜索引擎更友好

2013年12月06日 ⁄ 综合 ⁄ 共 3338字 ⁄ 字号 评论关闭

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

原始页面的HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Top.aspx.cs" Inherits="Admin_Top" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>调整ViewState的位置,让你的asp.net页面对搜索引擎更友好</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
    
</div>
    
</form>
</body>
</html>

这个页面后台没有任何业务cs代码的情况下,得到的HTML代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
    调整ViewState的位置,让你的asp.net页面对搜索引擎更友好
</title></head>
<body>
    
<form name="form1" method="post" action="Top.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGTKRk3xYdpqlKIfqyg44evx9dxYpQ==" />
</div>

    <div>
    
    
</div>
    
</form>
</body>
</html>

现在不改变前台aspx代码,重写Render方法,后台cs代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Admin_Top : 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)
    {

    }
}

最后生成的Html页面的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
    调整ViewState的位置,让你的asp.net页面对搜索引擎更友好
</title></head>
<body>
    
<form name="form1" method="post" action="Top.aspx" id="form1">
<div>

</div>

    <div>
    
    
</div>
    
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGTKRk3xYdpqlKIfqyg44evx9dxYpQ==" /></form>
</body>
</html>

最后的结果大家也看到了,确实移动了ViewState的html标记的位置,这样对搜索引擎更友好。

抱歉!评论已关闭.