MVC 3 Razor Meta描述/关键字

时间:2011-02-10 09:44:16

标签: asp.net-mvc-3 razor meta

在我的_Layout页面上获取元描述/关键字的最简单方法是什么? 类似于你获得页面标题的东西会很好。如

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

我的观点看起来像:

@{
    ViewBag.Title = "Contact Me";
    ViewBag.Keywords = "My, keyword, list, etc";
}

2 个答案:

答案 0 :(得分:4)

您的视图不应对此负责(不要向视图添加逻辑)。相反,它是应该指定元信息的动作方法。

在此处查看我的答案:asp.net mvc - strategy for including SEO information such as meta keywords and descriptions

答案仍然适用,但您应使用ViewBag代替ViewData

答案 1 :(得分:4)

我写了一个自定义mvc3 metatag帮助器就可以做到这一点。当使用多种语言时(如果需要),它可以使用资源:

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
        }
    }

然后在你的_Layout.cshtml文件中使用它: ...

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

@Html.MetaTag(MetatagType.MetaData).Render()

...

在您的资源文件中,您应添加以下内容(Controller-&gt; Home,Action-&gt; Index):

mt_home - &gt;主页标题

md_home - &gt;这里的主页介绍

mk_home - &gt;主页关键字

关于页面 mt_home_about - &gt;关于标题 等

您还可以选择在特定视图中覆盖资源中的元数据,如下所示 Index.cshtml:

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