Description / Keywords MVC 3 Razor Meta

What is the easiest way to get meta description / keywords on my _Layout page? Something similar to how you get the page title would be nice. For instance,

<title>@ViewBag.Title</title> <meta name="keywords" content='@ViewBag.Keywords'/> <meta name="description" content='@ViewBag.Description'/> 

My view would look something like this:

 @{ ViewBag.Title = "Contact Me"; ViewBag.Keywords = "My, keyword, list, etc"; } 
+4
source share
2 answers

Your opinion should not be responsible for this (do not add logic to the views). Instead, it is an action method that should indicate meta information.

See my answer here: asp.net mvc - strategy for incorporating SEO information such as meta keywords and descriptions

The answer still applies, although you should use ViewBag instead of ViewData .

+4
source

I wrote a special metatag mvc3 helper that will do just that. It works with resources when using multiple languages ​​(if required):

 public enum MetatagType { /// <summary> /// Used for Title meta tag. /// </summary> Title, /// <summary> /// Used for Description and keyword meta tag. /// </summary> MetaData, /// <summary> /// Used for noindex, nofollow meta tag. /// </summary> Robots } public static class MetatagExtensions { #region Public Methods public static MvcMetaTag MetaTag(this HtmlHelper helper, MetatagType metaType) { return new MvcMetaTag(helper, metaType); } #endregion public sealed class MvcMetaTag { #region Constructors and Destructors public MvcMetaTag(HtmlHelper helper, MetatagType metaType) { this.Helper = helper; this.MetaType = metaType; } #endregion #region Properties /// <summary> /// Gets or sets Helper. /// </summary> private HtmlHelper Helper { get; set; } /// <summary> /// Gets or sets MetaType. /// </summary> private MetatagType MetaType { get; set; } #endregion #region Public Methods public MvcHtmlString Render() { var sb = new StringBuilder(); if (this.Helper.ViewContext.RouteData.DataTokens.ContainsKey("area")) { sb.Append(this.Helper.ViewContext.RequestContext.RouteData.DataTokens["area"].ToString().ToLower()); sb.Append('_'); } sb.Append(this.Helper.ViewContext.RequestContext.RouteData.Values["controller"].ToString().ToLower()); if (this.Helper.ViewContext.RequestContext.RouteData.Values["action"].ToString().ToLower() != "index") { sb.Append('_'); sb.Append(this.Helper.ViewContext.RequestContext.RouteData.Values["action"].ToString().ToLower()); } var resourcemng = SharedResources.ResourceManager; var meta = new StringBuilder(); switch (this.MetaType) { case MetatagType.MetaData: meta.AppendLine( String.Format( "<meta name=\"description\" content=\"{0}\" />", this.Helper.ViewData["Description"] ?? resourcemng.GetString(string.Format("md_{0}", sb)) ?? string.Empty)); meta.AppendLine( String.Format( "<meta name=\"keywords\" content=\"{0}\" />", this.Helper.ViewData["Keywords"] ?? resourcemng.GetString(string.Format("mk_{0}", sb)) ?? string.Empty)); break; case MetatagType.Robots: meta.AppendLine( String.Format( "<meta name=\"robots\" content=\"{0}\" />", this.Helper.ViewData["Robots"] ?? resourcemng.GetString(string.Format("mr_{0}", sb)) ?? string.Empty)); break; case MetatagType.Title: meta.AppendLine( (string)(this.Helper.ViewData["Title"] ?? resourcemng.GetString(string.Format("mt_{0}", sb)) ?? string.Empty)); break; } return new MvcHtmlString(meta.ToString()); } #endregion } } 

Then in your _Layout.cshtml file you use it like this: ...

  <head runat="server"> <title>@Html.MetaTag(MetatagType.Title).Render()</title> @Html.MetaTag(MetatagType.MetaData).Render() 

...

In your resource file, you should add the following (Controller-> Home, Action-> Index):

mt_home → Homepage Title

md_home → description of the home page here

mk_home → home keywords

for the page mt_home_about → about the name, etc.

You also have the option of overwriting metadata from resources in specific views, such as Index.cshtml:

 @{ ViewBag.Title = "New TITLE"; ViewBag.Description = "New Description"; } 
+4
source

Source: https://habr.com/ru/post/1339158/


All Articles