protectedvoid Page_Load(object sender, EventArgs e) { HtmlMeta tag =new HtmlMeta(); tag.Name ="description"; tag.Content ="My description for this page"; Header.Controls.Add(tag); }
VB
Sub Page_Load()Sub Page_Load(ByVal sender AsObject, ByVal e As EventArgs) Dim tag As HtmlMeta =New HtmlMeta() tag.Name ="description" tag.Content ="My description for this page" Header.Controls.Add(tag) End Sub
using System; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Text.RegularExpressions; /**////<SUMMARY> /// 为了给内容页增加meta标签而扩展基类 ///</SUMMARY> publicclass BasePage : Page { privatestring _keywords; privatestring _description; // 构造函数 // 增加一个Init的事件处理 public BasePage() { Init +=new EventHandler(BasePage_Init); } // 页将使用这个基类初始化 // 如果可用则增加meta关键字和meta描述 void BasePage_Init(object sender, EventArgs e) { if (!String.IsNullOrEmpty(Meta_Keywords)) { HtmlMeta tag =new HtmlMeta(); tag.Name ="keywords"; tag.Content = Meta_Keywords; Header.Controls.Add(tag); } if (!String.IsNullOrEmpty(Meta_Description)) { HtmlMeta tag =new HtmlMeta(); tag.Name ="description"; tag.Content = Meta_Description; Header.Controls.Add(tag); } } /**////<SUMMARY> /// 获取或设置页的meta关键字 ///</SUMMARY> publicstring Meta_Keywords { get { return _keywords; } set { // 删掉多余的空格 // 译者注:/s匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [/f/n/r/t/v]。 _keywords = Regex.Replace(value, "//s+", ""); } } /**////<SUMMARY> /// 获取或设置页的meta描述 ///</SUMMARY> publicstring Meta_Description { get { return _description; } set { // 删掉多余的空格 // 译者注:/s匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [/f/n/r/t/v]。 _description = Regex.Replace(value, "//s+", ""); } } }
VB
Imports System Imports System.Web.UI Imports System.Web.UI.HtmlControls Imports System.Text.RegularExpressions ' 为了给内容页增加meta标签而扩展基类 PublicClass BasePageClass BasePage Inherits Page Dim _keywords AsString Dim _description AsString ' 页将使用这个基类初始化 ' 增加一个Init的事件处理 PublicSub New()SubNew() AddHandler Init, New EventHandler(AddressOf BasePage_Init) End Sub ' 页将使用这个基类初始化 ' 如果可用则增加meta关键字和meta描述 Sub BasePage_Init()Sub BasePage_Init(ByVal sender AsObject, ByVal e As EventArgs) IfNotString.IsNullOrEmpty(Meta_Keywords) Then Dim tag As HtmlMeta =New HtmlMeta() tag.Name ="keywords" tag.Content = Meta_Keywords Header.Controls.Add(tag) EndIf IfNotString.IsNullOrEmpty(Meta_Description) Then Dim tag As HtmlMeta =New HtmlMeta() tag.Name ="description" tag.Content = Meta_Description Header.Controls.Add(tag) EndIf End Sub '获取或设置页的meta关键字 PublicProperty Meta_Keywords()Property Meta_Keywords() AsString Get Return _keywords EndGet set ' 删掉多余的空格 ' 译者注:/s匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [/f/n/r/t/v]。 _keywords = Regex.Replace(value, "//s+", "") EndSet End Property ' 获取或设置页的meta描述 PublicProperty Meta_Description()Property Meta_Description() AsString Get Return _description EndGet Set(ByVal value AsString) ' 删掉多余的空格 ' 译者注:/s匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [/f/n/r/t/v]。 _description = Regex.Replace(value, "//s+", "") EndSet End Property End Class
<%@ Page Language="C#" MasterPageFile="~/PageTags.master" AutoEventWireup="true" CodeFile="home.aspx.cs" Inherits="home" CodeFileBaseClass="BasePage" Title="My home page title" Meta_Keywords="page directive, extension, dotnet, asp.net" Meta_Description="This is the meta description for my home page." %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <h3>My home page content<h3> <p> This is the content on my home page. This page has an appropriate title tag and also has meta tags for keywords and description that are relative to this page. The title tag is essential to good search engine optimization and the meta description is the text that the search engine will display when your page is listed in search results. The title and meta description should be set specific to each page and should describe the content of the page. </p> </asp:Content>
<%@ Page Language="C#" MasterPageFile="~/IdeaScope.master" AutoEventWireup="true" CodeFile="is.aspx.cs" Inherits="_is" CodeFileBaseClass="BasePage" Title="Effective Customer Feedback Management, Improve Customer Commmunication" Meta_Keywords="Customer Feedback, Customer Opinion, feedback, opinion, idea, ideas, idea management, customer feedback management, product management, product manager, product marketing, product marketing manager" Meta_Description="IdeaScope is an on-demand and embedded solution that allows you to capture, prioritize and centrally manage customer feedback. Make your customer feedback process more efficient. Save timeand involve more stakeholders without significant cost." %>
还有另外一个问题就是,Visual Studio 2005不认识Meta_Keywords属性和Meta_Description属性。你如果在@Page指令中指定了这两个属性的话,将会看到这些属性的下面会出现红色的波浪线,VS2005会认为它们是无效的,但实际上它仍然可以正确的编译和运行。如果你不想看到这些错误的话,你可以在Visual Studio的schema里给@Page指令增加如下代码。
这些节点应该作为<xsd:complexType name="PageDef">的子节点被插入,如果你把Visual Studio 2005安装在默认路径,那么这个schema文件的路径则是
C:/Program Files/Microsoft Visual Studio 8/Common7/Packages/schemas/html/page_directives.xsd
本文示范了如何通过扩展@Page指令使其支持meta关键字和meta描述。你也可以使用相同的方法增加其他的meta标签。原码文件和示例项目包括了c#和vb两种语言。感谢Scott Guthrie的博客文章,Obsure but cool feature in ASP.NET 2.0一文为本解决方案提供了技术支持。