【问题标题】:Add Meta tag in View page using MVC-5使用 MVC-5 在查看页面中添加元标记
【发布时间】:2014-06-05 11:17:33
【问题描述】:

我在 _Layout.cshtml 母版页中有两个元标记,现在我想在someone.cshtml 视图页面中添加元标记。

我也尝试使用此代码

放入_layout.cshtml母版页@RenderSection("metatags",false);

@section metatags { <meta ... /> }一样输入someone.cshtml

但没有成功。

并尝试添加元标记 jquery,但效果不佳。

【问题讨论】:

标签: asp.net-mvc-5 nopcommerce


【解决方案1】:

它应该可以工作。

这是我的母版页_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    @RenderSection("metatags", false)
    <title>My ASP.NET Application</title>
</head>
<body>
    @RenderBody()
</body>
</html>

这是index.cshtml 文件

@section metatags
{
    <meta name="test" content="test"/>
    <meta name="test2" content="test"/>
}

<div>Page content</div>

结果

<html>
<head>
    <meta charset="utf-8">

    <meta name="test" content="test">
    <meta name="test2" content="test">

    <title>My ASP.NET Application</title>
</head>
<body>        

<div>Page content</div>    

</body>
</html>

【讨论】:

  • 是的,我使用了此代码但无法正常工作。见我在 _layout.cshtml { @RenderSection("元标签",false); } 并在someone.cshtml中使用@section metatags { }
  • 尝试将@RenderSection("metatags",false) 替换为@RenderSection("metatags", true) 以确保使用该部分。
  • 错误:未定义部分:“元标记”。在 _Layout.cshtml
  • 您有多个母版页吗?
  • 是的.. _Root.cshtml 并在此母版页中包含 _Root.Head.cshtml 并且我在 _Root.Head 中添加此 { @RenderSection("metatags",required : true);}。 cshtml,因为在此页面中已经添加了 head 标签以及脚本和 css
【解决方案2】:

我找到了一种现在可行的方法。

此方案多用于多个母版页时。

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

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

在视图或母版页中获取该视图所需的任何元信息。

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

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

【讨论】:

    【解决方案3】:

    如果使用嵌套布局,您需要遵循以下准则:

    @RenderSection in nested razor templates

    您使用的技术应该可以工作,如果不行,也许就是这个原因。

    但是在你自己的答案中查看预期用途,如果你只需要修改关键字和描述标签,那么 NopCommrece 中有 apis。

    在您的主布局中:

    <meta name="description" content="@(Html.NopMetaDescription())" />
    <meta name="keywords" content="@(Html.NopMetaKeywords())" />
    

    在客户端cshtml文件中

    @{
        Html.AddMetaDescriptionParts(Model.MetaDescription);
        Html.AddMetaKeywordParts(Model.MetaKeywords);
    }
    

    在 NopCommerce 代码中有很多这样的示例。

    【讨论】:

      【解决方案4】:

      here 是一个很好的示例,介绍了如何在 asp.net mvc 中动态创建元标记

      public string HomeMetaTags()
      {
          var strMetaTag = new StringBuilder();
          strMetaTag.AppendFormat(@"<meta content='{0}' name='Keywords'/>","Home Action Keyword");
          strMetaTag.AppendFormat(@"<meta content='{0}' name='Descption'/>", "Home Description Keyword");
          return strMetaTag.ToString();
      }
      
      public ActionResult Index()
      {
          ViewBag.Message = "Welcome to ASP.NET MVC!";
          ViewBag.MetaTag = HomeMetaTags(); 
          return View();
      }
      


      <head>
          <title>@ViewBag.Title</title>
          <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
          <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
         @Html.Raw(ViewBag.MetaTag)
      </head>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-03
        • 1970-01-01
        • 2016-04-13
        • 1970-01-01
        • 2020-10-02
        • 1970-01-01
        • 2015-04-11
        相关资源
        最近更新 更多