Mvc 部分未呈现在头部,而是呈现在主体中

Mvc section not rendered in head but in body

我在 _Layout 的头部部分遇到 @RenderSection 的奇怪行为。

@section AddToHead{ 
    <meta name="test" />
    <open-graph og-title="@Model.Test.OG.Title" og-image="@Model.Test.OG.Image" og-url="@Model.Test.OG.Url" og-type="@Model.Test.OG.Type"></open-graph>
}
  1. meta => 是普通的 html
  2. open-graph => 是一个标签助手,它 returns html

并添加到 _Layout

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    @await RenderSectionAsync("AddToHead", required: false)
</head>

我已经尝试使用 RenderSectionAsync 和 RenderSection。没有区别。

当我在页面上查看结果时,它如下(完全不同的结果)

查看源代码

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <meta name="test" />
    <div><meta property='og:title' content='TestTitle' /><meta property='og:type' content='Article' /><meta property='og:url' content='TestURL' /><meta property='og:image' content='TestBild' /></div>
</head>

开发者工具

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <meta name="test" />

</head>
<body>
  <div><meta property='og:title' content='TestTitle' /><meta property='og:type' content='Article' /><meta property='og:url' content='TestURL' /><meta property='og:image' content='TestBild' /></div>
</body>

Facebook 像 Developer Tools 一样看待我的网站。

我做错了什么?这可能吗?

开发者工具显示浏览器如何解释您的HTML,这就是为什么您看到查看源代码和开发者工具之间存在差异的原因。

至于为什么会这样,<head> 中的 <div> 标签有问题。如今大多数浏览器都会将这些 <div> 标签解释为就好像它们在正文中一样。如果您要在没有周围 div 的情况下呈现 <meta /> 标签,一切都应该没问题。