【问题标题】:What's a good way to do meta tags in asp.net mvc 3?在 asp.net mvc 3 中做元标记的好方法是什么?
【发布时间】:2011-07-24 23:27:54
【问题描述】:

我目前有一个带有默认标题和元内容的主布局页面。如何覆盖子页面中的标题和元内容?

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    我找到了一种适用于现在的方法。我相信它可以改进,但这是我遇到的理想解决方案的壁橱。

    在控制器中分配该视图所需的任何元信息。

    ViewBag.Title = "some title";
    ViewBag.MetaDescription = "some description";
    etc...
    

    在主布局页面中,我执行以下操作

       <title>@ViewBag.Title</title>    
    
        @if(ViewBag.MetaDescription != null)
        {
            <meta name="description" content="@ViewBag.MetaDescription" />
        }
    
        @if(ViewBag.MetaKeywords != null)
        {
            <meta name="keywords" content="@ViewBag.MetaKeywords" />
        }
    

    【讨论】:

      【解决方案2】:

      定义一个ContentPlaceHolder(Razor 中的@RenderSection)并使用内容(Razor 中的@section)将其填充到使用母版页的视图中。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      相关资源
      最近更新 更多