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

本文介绍了一种在ASP.NET中将ViewState的HTML标记从页面顶部移至底部的方法,通过重写页面的Render方法实现,有助于提高网页对搜索引擎的友好度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

原始页面的HTML:

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

<! DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//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代码如下:



<! DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//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 RegexviewStateRegex = new Regex( @" (<inputtype=""hidden""name=""__VIEWSTATE""id=""__VIEWSTATE""value=""[w+//=]+""/>) " ,RegexOptions.Multiline | RegexOptions.Compiled);
// </form>标记的正则表达式
private static readonly RegexendFormRegex = new Regex( @" </form> " ,RegexOptions.Multiline | RegexOptions.Compiled);

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

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

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

}
protected void Page_Load( object sender,EventArgse)
{

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


<! DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//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标记的位置,这样对搜索引擎更友好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值