title:网站标题。keywords:网站关键字。description:网站的说明。
这三个元素在网站SEO中占有重要地位。搜索引擎会根据这些元素来识别网页描述的主题、内容。从而用户搜索的时候根据该信息判断是否是用户需要的内容而显示你的网站链接,以及把链接显示在哪个位置(搜索排行)。
title、keywords、description每个元素具体设置多长文字、多少个词、怎么样设置,是SEO的一个重要主题,本文就不讨论了,有兴趣的朋友可以到google搜索下“seo title keywords description”。
在asp.net开发网站中,实现全站统一设置这3个元素是很重要的。也是非常方便的。首先建立Site1.master模板页。根据该模板页,统一整个网站风格。分成3各部分:头、内容、尾。
代码如下:
<% @ Master Language = " C# " AutoEventWireup = " true " CodeBehind = " Site1.master.cs " Inherits = " SvnHostMaster.Site1 " %>
<! 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 " >
< link href = " http://www.svnhost.cn/style/public.css " rel = " stylesheet " type = " text/css " />
<%= Titlestr %>
<%= KeyStr %>
<%= DescStr %>
</ head >
< body >
<!-- 头开始 -->
< div >
< a href = " http://www.svnhost.cn " target = " _blank " >< img src = " http://www.svnhost.cn/images/logo.gif " alt = " 免费Svn托管服务 " /></ a >
< script type = " text/JavaScript " >
alimama_pid = " mm_10953762_962810_2022809 " ;
alimama_titlecolor = " 0000FF " ;
alimama_descolor = " 000000 " ;
alimama_bgcolor = " FFFFFF " ;
alimama_bordercolor = " E6E6E6 " ;
alimama_linkcolor = " 008000 " ;
alimama_bottomcolor = " FFFFFF " ;
alimama_anglesize = " 0 " ;
alimama_bgpic = " 0 " ;
alimama_icon = " 0 " ;
alimama_sizecode = " 11 " ;
alimama_width = 760 ;
alimama_height = 90 ;
alimama_type = 2 ;
</ script >
< script src = " http://a.alimama.cn/inf.js " type = text / javascript >
</ script >
</ div >
< div id = " navcontainer " >
< ul id = " navlist " >
< li id = " active " >< a href = " / " title = " SVN首页 " > 首页 </ a ></ li >
< li >< a href = " /Login.aspx " title = " SVN开源项目 " > 项目 </ a ></ li >
< li >< a href = " /News.aspx " title = " SVN文章 " > 文章 </ a ></ li >
< li >< a href = " /Login.aspx " title = " 下载 " > 下载 </ a ></ li >
< li >< a href = " /Login.aspx " title = " SVN管理中心 " > 管理 </ a ></ li >
< li >< a href = " http://www.svnhost.cn " title = " SVN帮助中心 " target = " _blank " > 帮助 </ a ></ li >
</ ul >
</ div >
<!-- 头结束 -->
<!-- 内容开始 -->
< div >
< asp:ContentPlaceHolder ID = " ContentPlaceHolder1 " runat = " server " >
</ asp:ContentPlaceHolder >
</ div >
<!-- 内容结束 -->
<!-- 尾开始 -->
< div class = " clearing " ></ div >
< div id = " index_foot " >< a href = " # " > 关于我们 </ a > - < a href = " mailto:quxiaohui_0@163.com " > 联系我们 </ a > - < a href = " /Help/1.shtml " > 帮助文档 </ a > - < a href = " mailto:quxiaohui_0@163.com?subject=Bug Report " > Bug Report </ a > - < a href = " # " title = " 开源社区QQ群 " > QQ群: 49745612 </ a ></ div >
< div id = " copyright " >
< p > Copyright & copy; 2006 - 2008 www.svnhost.cn < a href = " http://www.miibeian.gov.cn/ " target = " _blank " > 浙ICP备08006946号 </ a ></ p >
< p > 本站文章版权所有、转载请注明出处。 </ p >
</ div >
<!-- 尾结束 -->
</ body >
</ html >
这里添加了3个属性:Titlestr、KeyStr、DescStr,后台代码如下:
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;
namespace SvnHostMaster
{
public partial class Site1 : System.Web.UI.MasterPage
{
private string _KeyStr = null , _Titlestr = " 专业SVN托管服务|www.svnhost.cn " , _DescStr = null ;
public int nav;
public string NavStr( int id)
{
return nav == id ? " id=/"active/" " : "" ;
}
protected void Page_Load( object sender, EventArgs e)
{
}
public string DescStr
{
set { _DescStr = value; }
get
{
if (_DescStr != null )
return string .Format( " <meta name=/"description/" content=/"{0}/" /> " , Server.HtmlEncode(_DescStr));
else
return null ;
}
}
public string KeyStr
{
set { _KeyStr = value; }
get
{
if (_KeyStr != null )
return string .Format( " <meta name=/"keyword/" content=/"{0}/" /> " , Server.HtmlEncode(_KeyStr));
else
return null ;
}
}
public string Titlestr
{
set
{
_Titlestr = value;
}
get
{
return string .Format( " <title>{0}</title> " , Server.HtmlEncode(_Titlestr));
}
}
}
}
接下来就可以添加具体页面了。例如login.aspx,添加好以后后台代码就可以设置title、keywords、description这3个属性了。
代码如下:
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;
namespace SvnHostMaster
{
public partial class login : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
((Site1) this .Master).Titlestr = " 登录 - 专业SVN托管服务-源代码版本管理-SVN Hosting Service " ;
((Site1) this .Master).KeyStr = " Subversion 开源项目 开源软件 源代码 " ;
}
}
}
详细代码下载:asp.net网站中,全站统一设置title,keywords,description的实现方案
这样在每个页面里设置下标题、关键字和描述即可。还算比较方便。