Meta_Keywords属性和Meta_Description属性是公共的,你可以在类实例化后设置它们。当某个类继承自这个类并被初始化后,Base_Init将被调用并在页中增加meta标签
C#
public partial class home : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}VB
Partial Class homeClass home Inherits BasePage Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) End Sub End Class
注意每一个继承自BasePage的页都可以通过属性或代码来插入meta标签。现在我们可以直接在.aspx文件的@Page指令中指定Meta_Keywords属性和Meta_Description属性的值。示例如下
<%@ 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>
注意这里增加了一个CodeFileBaseClass属性,这是必需的,它可以引用BasePage类的公共属性
重点提要
你应该注意到了,在BasePage类里使用了正则表达式。这是因为在你的.aspx里添加描述和关键字的时候可能会是多行,就像下面这个例子似的
<%@ 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 time and involve more stakeholders without significant cost." %>
如果不用正则表达式转换它们的话,这些标记就会包含很多的新行和空格,这会使一些搜索引擎不知所措,所以我们要让这些标记方便搜索引擎的收录。
还有另外一个问题就是,Visual Studio 2005不认识Meta_Keywords属性和Meta_Description属性。你如果在@Page指令中指定了这两个属性的话,将会看到这些属性的下面会出现红色的波浪线,VS2005会认为它们是无效的,但实际上它仍然可以正确的编译和运行。如果你不想看到这些错误的话,你可以在Visual Studio的schema里给@Page指令增加如下代码。
<xsd:attribute name="Meta_Keywords" vs:nonfilterable="true" /> <xsd:attribute name="Meta_Description" vs:nonfilterable="true" />
这些节点应该作为<xsd:complexType name="PageDef">的子节点被插入,如果你把Visual Studio 2005安装在默认路径,那么这个schema文件的路径则是
C:Program FilesMicrosoft Visual Studio 8Common7Packagesschemashtmlpage_directives.xsd
本文示范了如何通过扩展@Page指令使其支持meta关键字和meta描述。你也可以使用相同的方法增加其他的meta标签。原码文件和示例项目包括了c#和vb两种语言。感谢Scott Guthrie的博客文章,Obsure but cool feature in ASP.NET 2.0一文为本解决方案提供了技术支持。
你的评论和建议将会证明本文是受欢迎的
上一篇:瑞星制造行业信息安全立体解决方案
发表评论