SeoTags
SeoTags生成所需的所有SEO标签,例如Meta , Link , Twitter卡(Twitter :),打开图(Facebook) (OG)(OG)和JSON-LD架构(结构化数据)。
如何使用
有关更多信息,请参见https://mjebrahimi.*gi**thub.io/seotags/。
1。安装软件包
PM> Install-Package SeoTags
2。注册/配置到您的服务
您需要做的一切都是配置SeoInfo对象并将其渲染到_Layout.cshtml中。
可以通过以三种方式设置SeoInfo对象的属性来完成此配置:
- 使用
services.Add SeoTags (seoInfo => { ... })方法在注册服务时。 -
.cshtml视图中的Html.SetSeoInfo(seoInfo => { ... })方法(mvc或razorPages) -
HttpContext.SetSeoInfo(seoInfo => { ... })方法在您访问HttpContext对象的任何地方(例如,在MVC控制器/ Action或Razor-pages PageModel中)
整个网站(不是特定于某个页面)的一般选项是恒定的,例如网站标题, Twitter ID , Facebook ID , OpenSearch URL , feeds(RSS或ATOM)等…
通常,使用services.Add SeoTags (seoInfo => { ... })方法在注册服务时设置这些值。
SeoTags(seoInfo =>
{
seoInfo.SetSiteInfo(
siteTitle: \”My Site Title\”,
siteTwitterId: \”@MySiteTwitter\”, //Optional
siteFacebookId: \”https://*faceb*ook*.com/MySite\”, //Optional
openSearchUrl: \”https://*sit*e*.com/open-search.xml\”, //Optional
robots: \”index, follow\” //Optional
);
//Optional
seoInfo.AddFeed(
title: \”Post Feeds\”,
url: \”https://site.c*om/**rss/\”,
feedType: FeedType.Rss);
//Optional
seoInfo.AddDnsPrefetch(\”https://fonts.*gstat*i*c.com/\”, \”https://www.googl*e-a**nalytics.com\”);
//Optional
seoInfo.AddPreload(new Preload(\”https://sit**e.com*/site.css\”),
new Preload(\”https://si*te.*co*m/app.js\”),
new Preload(\”https://si*te.co*m/*fonts/Font.woff2\”),
new Preload(\”https://sit*e.com**/fonts/Font_Light.woff2\”),
new Preload(\”https://s*i*t*e.com/fonts/Font_Medium.woff2\”),
new Preload(\”https://sit*e*.*com/fonts/Font_Bold.woff2\”));
//Optional
seoInfo.SetLocales(\”en_US\”);
});
//…\”>
//Register your services app . Services . Add SeoTags ( seoInfo => { seoInfo . SetSiteInfo ( siteTitle : \"My Site Title\" , siteTwitterId : \"@MySiteTwitter\" , //Optional siteFacebookId : \"https://*faceb*ook*.com/MySite\" , //Optional openSearchUrl : \"https://*sit*e*.com/open-search.xml\" , //Optional robots : \"index, follow\" //Optional ) ; //Optional seoInfo . AddFeed ( title : \"Post Feeds\" , url : \"https://site.c*om/**rss/\" , feedType : FeedType . Rss ) ; //Optional seoInfo . AddDnsPrefetch ( \"https://fonts.*gstat*i*c.com/\" , \"https://www.googl*e-a**nalytics.com\" ) ; //Optional seoInfo . AddPreload ( new Preload ( \"https://sit**e.com*/site.css\" ) , new Preload ( \"https://si*te.*co*m/app.js\" ) , new Preload ( \"https://si*te.co*m/*fonts/Font.woff2\" ) , new Preload ( \"https://sit*e.com**/fonts/Font_Light.woff2\" ) , new Preload ( \"https://s*i*t*e.com/fonts/Font_Medium.woff2\" ) , new Preload ( \"https://sit*e*.*com/fonts/Font_Bold.woff2\" ) ) ; //Optional seoInfo . SetLocales ( \"en_US\" ) ; } ) ; //...
3。在您的_layout.cshtml中渲染seo标签
要渲染输出SEO标签,请致电Html. SeoTags () _Layout.cshtml中的Html. SeoTags ()方法。
此方法具有两个过载,一个具有SeoInfo参数(如果您需要传递SeoInfo对象的新任意实例),一个没有参数,可以从您以前的注册服务中检索已配置的SeoInfo对象。
–>
<!– <span class=\’ notranslate keguazi>SeoTags generates all of these for you –>
@Html. SeoTags () <!– ? Add this line –>\”>
<!DOCTYPE html > < html lang =\" en \" > < head > <!-- Remove these tags from your _Layout.cshtml <meta charset=\"utf-8\" /> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /> <title>@ViewData[\"Title\"] - Site Title</title> --> <!-- SeoTags generates all of these for you --> @Html. SeoTags () <!-- ? Add this line -->
4。在您的视图/页面中设置特定的SEO信息
您可能需要为某个页面设置一些特定的SEO信息,例如页面标题,页面描述,页面关键字,页面URL ,发布日期,修改日期,图像信息,页面类型等…
为此,请在.cshtml视图中调用Html.SetSeoInfo(seoInfo => { ... })方法,以设置该页面的特定所需的SEO信息。
您可以通过调用HttpContext.SetSeoInfo(seoInfo => { ... })来执行相同的操作。您可以在访问HttpContext对象的任何地方(例如,在MVC Controller / Action或Razor-pages PageModel中)
SeoTags.TwitterCardType.SummaryLargeImage //Optional
);
seoInfo.SetArticleInfo(
authorName: \”Author Name\”,
publishDate: DateTimeOffset.Now,
modifiedDate: DateTimeOffset.Now, //Optional
authorTwitterId: \”@MyTwitterId\”, //Optional
authorFacebookId: \”https://f**a*cebook.com/MyUserId\”, //Optional
authorUrl: \”https://**github.c*om/author-profile\”, //Optional
section: \”Article Topic\” //Optional
);
//Add another RSS feed. (only for this page) (Optional)
seoInfo.AddFeed(\”Post Comments\”, \”https://*site.*c*om/post/comment/rss\”, SeoTags.FeedType.Rss);
});
}\”>
@ { // Remove these line from your views // ViewData[\"Title\"] = \"Page Title\"; Html . SetSeoInfo ( seoInfo => { seoInfo . SetCommonInfo ( pageTitle : \"SEO Tags for ASP.NET Core\" , description : \"SetoTags creates all SEO tags you need such as meta, link, Twitter card (twitter:), open graph (og:), and ...\" , url : \"https://si*t**e.com/url/\" , keywordTags : new [ ] { \"SEO\" , \"AspNetCore\" , \"MVC\" , \"RazorPages\" } , //Optional seeAlsoUrls : new [ ] { \"https://sit*e.*co*m/see-also-1\" , \"https://site*.c*om*/see-also-2\" } //Optional ) ; seoInfo . SetImageInfo ( url : \"https://site.co*m/*u*ploads/image.jpg\" , width : 1280 , //Optional height : 720 , //Optional alt : \"Image alt\" , //Optional //mimeType: \"image/jpeg\", //Optional (detects from URL file extension if not set.) cardType : SeoTags . TwitterCardType . SummaryLargeImage //Optional ) ; seoInfo . SetArticleInfo ( authorName : \"Author Name\" , publishDate : DateTimeOffset . Now , modifiedDate : DateTimeOffset . Now , //Optional authorTwitterId : \"@MyTwitterId\" , //Optional authorFacebookId : \"https://f**a*cebook.com/MyUserId\" , //Optional authorUrl : \"https://**github.c*om/author-profile\" , //Optional section : \"Article Topic\" //Optional ) ; //Add another RSS feed. (only for this page) (Optional) seoInfo . AddFeed ( \"Post Comments\" , \"https://*site.*c*om/post/comment/rss\" , SeoTags . FeedType . Rss ) ; } ) ; }
5。完成!享受渲染输出
在浏览器中打开页面并查看源代码。
以下代码显示了此示例的渲染输出。
<!DOCTYPE html > < html lang =\" en \" > < head > < meta charset =\" utf-8 \" /> < meta http-equiv =\" Content-Type \" content =\" text/html; charset=utf-8 \" /> < meta http-equiv =\" X-UA-Compatible \" content =\" IE=edge, chrome=1 \" /> < meta name =\" viewport \" content =\" width=device-width, initial-scale=1 \" /> < link rel =\" preconnect \" href =\" https://fonts.*gstat*i*c.com/ \" crossorigin /> < link rel =\" preconnect \" href =\" https://www.googl*e-a**nalytics.com \" crossorigin /> < link rel =\" dns-prefetch \" href =\" https://fonts.*gstat*i*c.com/ \" /> < link rel =\" dns-prefetch \" href =\" https://www.googl*e-a**nalytics.com \" /> < link rel =\" preload \" as =\" style \" href =\" https://sit**e.com*/site.css \" /> < link rel =\" preload \" as =\" script \" href =\" https://si*te.*co*m/app.js \" /> < link rel =\" preload \" as =\" font \" type =\" font/woff2 \" href =\" https://si*te.co*m/*fonts/Font.woff2 \" crossorigin /> < link rel =\" preload \" as =\" font \" type =\" font/woff2 \" href =\" https://sit*e.com**/fonts/Font_Light.woff2 \" crossorigin /> < link rel =\" preload \" as =\" font \" type =\" font/woff2 \" href =\" https://s*i*t*e.com/fonts/Font_Medium.woff2 \" crossorigin /> < link rel =\" preload \" as =\" font \" type =\" font/woff2 \" href =\" https://sit*e*.*com/fonts/Font_Bold.woff2 \" crossorigin /> < link rel =\" preload \" as =\" image \" type =\" image/jpeg \" href =\" https://site.co*m/*u*ploads/image.jpg \" /> < title > SEO Tags for ASP.NET Core - My Site Title </ title > < meta name =\" title \" content =\" SEO Tags for ASP.NET Core - My Site Title \" /> < meta name =\" description \" content =\" SetoTags Creates all SEO tags you need such as meta, link, Twitter card (twitter:), open graph (og:), and ... \" /> < meta name =\" keywords \" content =\" SEO, AspNetCore, MVC, RazorPages \" /> < meta name =\" author \" content =\" Author Name \" /> < link rel =\" author \" href =\" https://**github.c*om/author-profile \" /> < link rel =\" canonical \" href =\" https://si*t**e.com/url/ \" /> < link rel =\" application/opensearchdescription+xml \" title =\" My Site Title \" href =\" https://*sit*e*.com/open-search.xml \" /> < link rel =\" alternate \" type =\" application/rss+xml \" title =\" Post Feeds \" href =\" https://site.c*om/**rss/ \" /> < link rel =\" alternate \" type =\" application/rss+xml \" title =\" Post Comments \" href =\" https://*site.*c*om/post/comment/rss \" /> < meta name =\" twitter:card \" content =\" summary_large_image \" /> < meta name =\" twitter:title \" content =\" SEO Tags for ASP.NET Core \" /> < meta name =\" twitter:description \" content =\" SetoTags creates all SEO tags you need such as meta, link, Twitter card (twitter:), open graph (og:), and ... \" /> < meta name =\" twitter:site \" content =\" @MySiteTwitter \" /> < meta name =\" twitter:creator \" content =\" @MyTwitterId \" /> < meta name =\" twitter:image \" content =\" https://site.co*m/*u*ploads/image.jpg \" /> < meta name =\" twitter:image:width \" content =\" 1280 \" /> < meta name =\" twitter:image:height \" content =\" 720 \" /> < meta name =\" twitter:image:alt \" content =\" Image alt \" /> < meta property =\" og:type \" content =\" article \" /> < meta property =\" og:title \" content =\" SEO Tags for ASP.NET Core \" /> < meta property =\" og:description \" content =\" SetoTags creates all SEO tags you need such as meta, link, Twitter card (twitter:), open graph (og:), and ... \" /> < meta property =\" og:url \" content =\" https://si*t**e.com/url/ \" /> < meta property =\" og:site_name \" content =\" My Site Title \" /> < meta property =\" og:locale \" content =\" en_US \" /> < meta property =\" og:image \" content =\" https://site.co*m/*u*ploads/image.jpg \" /> < meta property =\" og:image:secure_url \" content =\" https://site.co*m/*u*ploads/image.jpg \" /> < meta property =\" og:image:type \" content =\" image/jpeg \" /> < meta property =\" og:image:width \" content =\" 1280 \" /> < meta property =\" og:image:height \" content =\" 720 \" /> < meta property =\" og:image:alt \" content =\" Image alt \" /> < meta property =\" article:publisher \" content =\" https://*faceb*ook*.com/MySite \" /> < meta property =\" article:author \" content =\" https://f**a*cebook.com/MyUserId \" /> < meta property =\" article:published_time \" content =\" 2021-07-03T13:34:41+00:00 \" /> < meta property =\" article:modified_time \" content =\" 2021-07-03T13:34:41+00:00 \" /> < meta property =\" article:section \" content =\" Article Topic \" /> < meta property =\" article:tag \" content =\" SEO \" /> < meta property =\" article:tag \" content =\" AspNetCore \" /> < meta property =\" article:tag \" content =\" MVC \" /> < meta property =\" article:tag \" content =\" RazorPages \" /> < meta property =\" og:see_also \" content =\" https://sit*e.*co*m/see-also-1 \" /> < meta property =\" og:see_also \" content =\" https://site*.c*om*/see-also-2 \" /> ...
JSON-LD支持
SeoTags现在支持流行的JSON-LD类型,例如文章,产品,书籍,组织,网站,网页等…
请参阅我们的文档以获取嵌套示例和引用示例。
笔记
- 该软件包不会生成favicon标签。使用realfavicongoner.net生成favicon标签。
- 仅支持ASP.NET核心(而不是遗留ASP.NET框架)
贡献
如果您找到错误或有建议或问题,请创建问题。如果您想开发此项目:
- 叉!
- 创建您的功能分支:
git checkout -b my-new-feature - 提交您的更改:
git commit -am \'Add some feature\' - 推到分支机构:
git push origin my-new-feature - 提交拉请请求
给星星!配x
如果您发现此存储库有用,请给它一颗星。谢谢!
执照
版权所有©2021 Mohammd Javad Ebrahimi在MIT许可下。
