用於處理ASP.NET Web應用程序的SEO數據的幫助者。
提供一個可在Controllers , Views和通過ActionFilterAttributes中易於訪問的SeoHelper -Class。
SeoHelper -Class公開了多個屬性以獲取或設置多個SEO相關數據:
LinkCanonical :獲取或設置用於網頁的規範鏈接。
SiteUrl :獲取或設置Web站點的主要URL。用作規範鏈接的基礎。 MetaDescription :獲取或設置網頁的元描述。
MetaKeywords :獲取或設置用於網頁的元關鍵字。
MetaRobots :獲取或設置元機器人指令網頁。
OgDescription :獲取或設置Web頁面的打開圖形描述。在MetaDescription中恢復了價值。
OgImage :獲取或設置用於網頁的打開圖像。
OgSiteName :獲取或設置用於網頁的開放圖站點名稱。在SiteName中回到價值。
OgTitle :獲取或設置Web頁面的打開圖標題。在PageTitle價值上倒退。
OgType :獲取或設置用於網頁的打開圖類型。
OgUrl :獲取或設置用於網頁的打開圖URL。在LinkCanonical中恢復了價值。
PageTitle :獲取或設置網頁標題。
SiteName :獲取或設置Web站點的名稱。用作DocumentTitle的基礎。 DocumentTitle :獲取網頁的文檔標題。結合了PageTitle和SiteName 。
DocumentTitleFormat :獲取或設置文檔標題的格式。默認值為{0} - {1} ,其中{0}是來自PageTitle的值, {1}是SiteName的值。SetCustomMeta(string key, string value) :添加任何自定義元標記。SetMetaRobots(bool index, bool follow) :指定機器人的說明。更新MetaRobots的值。可以將LinkCanonical設置為絕對URL ( https://example.com/section/page.html ),作為相對URL ( /section/page.html )或使用ASP.NET的App.net app-interative url-format ( ~/section.page.html )。相對URL將自動從提供的SiteUrl或請求URL的底部轉換為絕對URL 。
SeoHelper -Class暴露的屬性均具有相應的動作過濾器 - 屬性,可用於控制器和控制器actions。
例如,如果不需要該值的邏輯,則可以將[PageTitle]用於操作,而[SiteName]可以用於控制器。在操作上使用另一個[SiteName] - 屬性將覆蓋控制器上使用的一個。
屬性 - 使用示例:
[SiteName("Website name")]
[SiteUrl("https://production-url.co/")]
public class InfoController : SeoController
{
[PageTitle("Listing items")]
[MetaDescription("List of the company's product-items")]
public ActionResult List()
{
var list = GetList();
if (list.Any())
{
Seo.PageTitle += $" (Total: {list.Count})";
Seo.LinkCanonical = "~/pages/list.html";
}
else
{
Seo.SetMetaRobots(index: false, follow: true);
}
return View(model);
}
}
要註冊SEO助手作為依賴項注入的服務,您只需要在Startup.cs內的ConfigureServices方法中使用框架提供的擴展名方法:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSeoHelper();
}
}
SeoHelper -Class使用已配置的依賴項注入,您可以通過構造函數注入或訪問HttpContext中的RequestServices -Object訪問SeoHelper -Class。
該框架為IServiceProvider提供了一種擴展方法,以獲取SeoHelper -instance:
public IActionResult Edit()
{
var seoHelper = HttpContext.RequestServices.GetSeoHelper();
// ...
}
標籤助手可用於渲染通過SeoHelper -Class設置的值。他們還將屬性暴露於覆蓋或在標記中即時設置值。
示例: <document-title />呈現組合的PageTitle和SiteName 。 <link-canonical />呈現頁面的規範URL。 <og-url />呈現頁面的設置URL,並落後於<link-canonical />中使用的值。
如果沒有通過SeoHelper -Class或裸露的屬性提供有效的數據,則不會呈現單個標籤。
為了設置Webb-application的默認基本標題和默認的基本規範鏈接,可以在需要時被覆蓋,可以使用註冊依賴項注入服務期間的配置:
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddSeoHelper(
siteName: "Website name",
siteUrl: "https://production-url.co/");
}
對經典ASP.NET MVC的支持已棄用。