65.9K
CodeProject 正在变化。 阅读更多。
Home

ASP.NET 4.0 中的搜索引擎优化 (SEO) 增强功能

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.47/5 (11投票s)

2010 年 6 月 9 日

CPOL

2分钟阅读

viewsIcon

37175

本文介绍了与 ASP.NET 4.0 中 SEO 增强功能相关的 MetaKeywords 和 MetaDescription 属性。

目录

引言

众所周知,Meta 描述 (META 描述标签) 从 SEO 的角度来看非常重要。META 描述标签是识别页面的重要组成部分,搜索引擎非常认真地对待此 META 标签。为了提高页面的搜索相关性,请确保您始终将相关的“keywords”和“description<meta> 标签放在 HTML 的 <head> 部分中。

<head runat="server">
    <title>SEO Demo</title>
    <meta name="description" content="Page keywords." />
    <meta name="keywords" content="ASP.NET 4, SEO enhancements." />
</head>

ASP.NET 4.0 中,一个显著的增强功能是向 Page 类添加了两个新属性:- MetaKeywordsMetaDescription。这些属性会将“keywords”和“description<meta> 标签自动放入页面的 <head> 部分。

何时有用?

当您的网站使用 母版页时,这特别有用 - 并且该部分最终位于 .master 文件中。现在,您可以在 .aspx 页面中设置新的 MetaKeywordsMetaDescription 属性,并且它们的值将由 母版页内的部分自动呈现。

使用 MetaKeywords 和 MetaDescription 属性

您可以通过 @Page 属性直接定义 Meta KeywordsMeta Description,如下所示:

<%@ Page
    Title="SEO Demo"
    Language="C#"
    MasterPageFile="~/MasterPage.master"
    AutoEventWireup="true"
    CodeFile="Default.aspx.cs"
    Inherits="_Default"
    MetaKeywords="Page key words"
    MetaDescription="Page description"
%> 

或者您也可以在 Page 类的 Page_Load 事件中以编程方式定义相同的内容,如下所示:

protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.MetaKeywords = "Page key words";
        this.Page.MetaDescription = "Page description";
    }

通过以上两种方法,标记都会在 Web 浏览器中呈现为:

<head>
   <title>SEO Demo</title>
   <meta name="description" content="Page description" />
   <meta name="keywords" content="Page key words" />
</head>

需要注意的一个重要点是,如果您以编程方式设置值,则通过 节或 @Page 属性在声明中设置的值将被覆盖。

如果您想通过保留旧内容来更新 meta 标签内容,请使用以下技术:

protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.MetaKeywords += " - NewPage key words.";
        this.Page.MetaDescription += " - New Page description.";
    }

现在在 Web 浏览器中呈现的标记是:

<head>
   <title>SEO Demo</title>
   <meta name="description" content="Page description - New Page description."/>
   <meta name="keywords" content="Page key words - NewPage key words." />
</head>

总结

因此,这确实是 ASP.NET 4.0 中一个很好的增强功能,它为我们提供了更大的灵活性来定义 meta 标签内容。

历史记录

  • 2010年6月9日 - 文章已更新(添加了目录)
  • 2010年6月9日 - 发布原始版本
© . All rights reserved.