用于处理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的支持已弃用。