如何加Keyword 和 Description meta tags 到每个页面,看代码:
Code
//Change the line
// public partial class SomePage : System.Web.UI.Page
//to
// public partial class SomePage : PageEx
public partial class SomePage : PageEx
{
protected void Page_Load(object sender, EventArgs e)
{
Title = "Free Piston Power Pack (FP3)";
this.Description = "Your Description of the page";
this.Keywords = new string[] { "keyword1", "keyword1" ,"keyword1"};
}
}
//add the PageEx.cs in the App_Code Directory
using System;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// Summary description for PageExPage
/// </summary>
public class PageEx : System.Web.UI.Page
{
private HtmlMeta metaDescription = new HtmlMeta();
private HtmlMeta metaKeywords = new HtmlMeta();
public string Description
{
get { return metaDescription.Content; }
set { metaDescription.Content = value; }
}
public string[] Keywords
{
get
{
if (metaKeywords.Content == null)
{
return new string[] { "" };
}
else
{
return metaKeywords.Content.Split(new char[] { ',' });
}
}
set
{
if (value != null)
{
metaKeywords.Content = string.Join(",", value);
}
}
}
public PageEx()
{
Init += new EventHandler(PageEx_Init);
}
void PageEx_Init(object sender, EventArgs e)
{
//Add the description Meta control
metaDescription.Name = "description";
Page.Header.Controls.Add(metaDescription);
//Add the keywords Meta control
metaKeywords.Name = "keywords";
Page.Header.Controls.Add(metaKeywords);
}
}
文章出处:http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx 【直接拉到最后】