.net 为什么blazor HeadOutlet在App之后呈现

v1uwarro  于 2023-11-20  发布在  .NET
关注(0)|答案(2)|浏览(181)

我在服务器端使用HeadOutlet预渲染net6.0应用程序来设置一些头标签,如Meta描述,但服务器首先渲染应用程序,然后是头,这使得搜索引擎忽略它。

  1. @page "/"
  2. @namespace Example.Pages
  3. @using Microsoft.AspNetCore.Components.Web
  4. @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
  5. @{
  6. Layout = null;
  7. }
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
  12. <base href="~/" />
  13. <link href="css/site.css" rel="stylesheet" />
  14. <link rel="preconnect" href="https://fonts.gstatic.com">
  15. </head>
  16. <body>
  17. <component type="typeof(App)" render-mode="ServerPrerendered" />
  18. <div id="blazor-error-ui">
  19. <environment include="Staging,Production">
  20. An error has occurred. This application may no longer respond until reloaded.
  21. </environment>
  22. <environment include="Development">
  23. An unhandled exception has occurred. See browser dev tools for details.
  24. </environment>
  25. <a href="/" class="reload">Reload</a>
  26. <a href="#" class="dismiss">🗙</a>
  27. </div>
  28. <script src="_framework/blazor.server.js"></script>
  29. <script src="~/outsideHandleContainerJsInterop.js"></script>
  30. </body>
  31. </html>

个字符
在浏览器中查看页面将按照预期在头部呈现Meta标记,但在失眠/ Postman 中执行get请求将返回初始头部和blazor预呈现标记注解

  1. <!--Blazor:{"sequence":0,"type":"server","prerenderId":"b0376004567c4aaf9c07defc4341e21e","descriptor":"<long string here>"}--><!--Blazor:{"prerenderId":"b0376004567c4aaf9c07defc4341e21e"}-->


这是一个错误还是我错过了什么?我需要头部之前或与页面的其余部分呈现,以便搜索引擎可以拿起它。

laawzig2

laawzig21#

这是通过将html/head/body从_Host.cshtml移动到_Layout. html来解决的。更多信息在这篇文章中描述:https://github.com/dotnet/aspnetcore/issues/37293

hkmswyz6

hkmswyz62#

我没有一个_Layout文件。我没有移动,而是在_Host文件中完全限定了HeadOutlet的类并使其工作。

  1. <component type="typeof(Microsoft.AspNetCore.Components.Web.HeadOutlet)" render-mode="ServerPrerendered" />

字符串

相关问题