它使您可以轻松地将搜索引擎元数据添加到您的ASP.NET Core网站上,从而使您专注于编写要编写的代码。它提供了三件事:
<meta>标签您不再需要查找这些独立协议的每一个的格式,也不必用大量的<meta>标签污染您的HTML。
它还使您可以灵活地定义在运行时生成这些文件的行为。这很有用,因为这意味着您可以做一些事情,例如告知搜索引擎机器人不要爬行开发和登台站点,而是允许它们为生产版本索引,而无需编写和维护三个单独的文件。
将软件包Winton.AspNetCore.Seo添加到项目的.csproj文件或使用Nuget Package Manager安装:
Install-Package Winton.AspNetCore.Seo在Startup.cs中的ConfigureServices中添加所需的服务。例如,使用默认robots.txt设置,而不定义站点地图:
public void ConfigureServices ( IServiceCollection services )
{
services . AddSeoWithDefaultRobots ( ) ;
}将适当的打开图taghelper添加到您的HTML文件中。例如在_Layout.cshtml中:
@using System;
@using Winton.AspNetCore.Seo.HeaderMetadata.OpenGraph
@addTagHelper *, Winton.AspNetCore.Seo
<!DOCTYPE html >
< html >
< head >
< open-graph-article
audio =" @{ new Audio( " http: //example.com") }"
authors=" @{ new List<string> { " http: //example.com/choc13" } }"
description=" Test description "
images ="
@{
new List<Image>
{
new Image( " http: //example.com/img1.jpg") { SecureUrl = " https://example.com/img1.jpg " , Height = 250 },
new Image( "http://example.com/img2.jpg") { SecureUrl = " https://example.com/img2.jpg " , Height = 300, Width = 500 }
};
} "
published-time=" @{ DateTime.UtcNow } "
title =" Test "
>
</ open-graph-article >
</ head >
< body >
</ body >
</ html >当调用AddSeoWithDefaultRobots或AddSeo :
使用代表。例如:
services . AddSeoWithDefaultRobots (
options => options . Urls = new List < SitemapUrlOptions >
{
new SitemapUrlOptions
{
Priority = 0.9M ,
RelativeUrl = " test "
}
} ) ;当您想使用代码定义站点映射并保留默认robots.txt设置时,这很有用(有关自定义robots.txt选项的更多详细信息,请参见下文)。还有一个版本的代表可以访问IWebHostEnvironment ,以何时依赖于环境。
从配置。例如:
services . AddSeo ( _configuration ) ;当您想将站点地图放入配置(例如appsettings.json )时,这很有用。
配置必须包含一个带有密钥"Seo"部分,例如:
{
"Seo" : {
"Sitemap" : {
"Urls" : [
{
"Priority" : 0.9 ,
"RelativeUrl" : " /login "
},
{
"Priority" : 0.8 ,
"RelativeUrl" : " /about "
}
]
}
}
}请注意,此方法未设置任何默认robots.txt选项,因此您还需要根据应用程序的要求配置robots.txt。
robots.txt文件可以与站点地图相同的方式配置。如果将使用AddSeoWithDefaultRobots称为默认robots.txt选项,将应用以下策略:
注意:总是值得记住的是,恶意bot可以忽略一个机器人。txt文件,因此不应依靠隐藏站点的任何敏感部分。
改用AddSeo允许您指定选项。它们可以指定几种方法:
使用代表。例如:
public void ConfigureServices ( IServiceCollection services )
{
services . AddSeo (
options =>
{
options . RobotsTxt . AddSitemapUrl = false ;
options . RobotsTxt . UserAgentRecords = new List < UserAgentRecord >
{
new UserAgentRecord
{
Disallow = new List < string > { " /login " } ,
UserAgent = " bing "
}
} ;
} ) ;
}当您想使用代码定义robots.txt时,这很有用。您还可以在同一委托中配置站点地图。还有一个版本的代表可以访问IWebHostEnvironment ,以何时依赖于环境。
从配置。例如:
services . AddSeo ( _configuration ) ;当您要在config中定义robots.txt时,这很有用,例如appsettings.json 。
配置必须包含一个带有密钥"Seo"部分,例如:
{
"Seo" : {
"RobotsTxt" : {
"AddSitemapUrl" : false ,
"UserAgentRecords" : [
{
"UserAgent" : " bing " ,
"Disallow" : [ " /login " ]
},
{
"UserAgent" : " google " ,
"Disallow" : [ " * " ]
}
]
}
}
}如果需要站点地图,则还应通过配置配置站点地图。
RobotsTxtOptions具有以下属性:
AddSitemapUrl确定是否应将站点地图的URL包含在robots.txt文件中UserAgentRecords用户代理记录的集合。使用它来定义您希望每个特定机器人忽略的站点部分。您可以使用通配符用户代理来定义记录,以使用*将相同的规则应用于所有机器人。@addTagHelper *, Winton.AspNetCore.Seo import语句:
open-graph-articleopen-graph-bookopen-graph-profileopen-graph-websiteopen-graph-music-albumopen-graph-music-playlistopen-graph-music-radio-stationopen-graph-music-songopen-graph-video-episodeopen-graph-video-movieopen-graph-video-otheropen-graph-video-tv-show为了为本库定义的某些类型(例如Audio和Image )分配一些属性,您还需要在CSHTML文件的顶部添加@using Winton.AspNetCore.Seo.HeaderMetadata.OpenGraph 。
启动您的网站,您应该能够在浏览器中以下URL查看文件:
如果您添加了任何开放的图形标签助手,则网站的HTML页面的头部也应该有几个<meta>标签。
上面列出的所有开放图标签助手扩展了OpenGraphTagHelper类。要添加自己的开放图对象,您也可以扩展此类。基类将照顾将标签助手中的属性转换为正确的打开图元标记。您也可以在此库中使用OpenGraphPropertyAttribute和OpenGraphNamespaceAttribute微调对象的behvaiour。请参阅下面的示例。
默认情况下, OpenGraphTagHelper指定所有标签的og名称空间。要覆盖任何自定义开放图对象上的属性,只需将OpenGraphNamespaceAttribute添加到您的类中即可。例如: [OpenGraphNamespaceAttribute("example", "http://example.com/ns#")] 。
以下是自定义打开图标签助手的示例简介:
[ OpenGraphNamespaceAttribute ( " baz " , " http://baz.com/ns# " ) ]
public sealed class OpenGraphFooTagHelper : OpenGraphTagHelper
{
public OpenGraphFooTagHelper ( )
: base ( " foo " ) // Call the base constructor with the og:type of the custom object
{
}
// Property will render as <meta content="value assigned" property="baz:bar">
public string Bar { get ; set ; }
// Property will render as <meta content="value assigned" property="baz:foo_bar"> for each entry
// The OpenGraphPropetyAttribute can be used to specify the property name explicitly
[ OpenGraphProperty ( Name = " foo_bar " ) ]
public IEnumerable < string > FooBars { get ; }
}有关更多示例,请参见SRC,以了解如何实现此库提供的开放图对象。