网站标题,关键字等对一个网站的seo的重要性是不言而喻的。如果在网站的每个页面*.aspx管理,工作量非常大,这里提供一种方案.
1.首先定义一个SEO信息实体,一个页面的基类,让所有页面继承该基类
SEO实体:SiteSeoinf
代码
public class SiteSeoinf
{
public string Title
{
get;
set;
}
public string KeyWord
{
get;
set;
}
public string Desc
{
get;
set;
}
}
页面基类:BasePage
代码
public class BasePage : System.Web.UI.Page
{
public SiteSeoinf _seoinfo=new SiteSeoinf();
private string _titleFormat=null;
public BasePage()
{
}
/// <summary>
/// 页面加载前
/// </summary>
/// <param name="e"></param>
protected override void OnInit(EventArgs e)
{
_titleFormat = Application["WebName"].ToString() + " {0}";
//导入css
HtmlLink link = new HtmlLink();
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("href", " style/040/040.css");
this.Header.Controls.Add(link);
}
/// <summary>
/// 页面加载完成后
/// </summary>
/// <param name="e"></param>
protected override void OnSaveStateComplete(EventArgs e)
{
_seoinfo.Title = string.Format(_titleFormat, _seoinfo.Title);
this.Page.Title = _seoinfo.Title;
HtmlMeta meta = new HtmlMeta();
meta.Name = "keywords";
meta.Content = _seoinfo.KeyWord;
this.Header.Controls.Add(meta);
HtmlMeta metaDesc = new HtmlMeta();
metaDesc.Name = "description";
metaDesc.Content = _seoinfo.Desc;
this.Header.Controls.Add(metaDesc);
}
}
2.对应的页面继承该基类,并写入相应的标题,关键字,描述等信息即可
代码
public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
base._seoinfo.Title = " - 网站首页";
base._seoinfo.KeyWord = "这个一个学校首页";
}
}