它使您可以輕鬆地將搜索引擎元數據添加到您的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,以了解如何實現此庫提供的開放圖對象。