Blazor .NET Core 7要使用哪个布局文件? _host.cshtml和_layouts.cshtml。

huangapple go评论55阅读模式
英文:

Blazor .NET Core 7 which layout file to use? _host.cshtml and _layouts.cshtml

问题

我正在使用Blazor与.NET Core 7。默认项目带有_hosts文件。但是cshtml文件不使用_host而是使用_layouts。我看过一些项目将_host文件指向了_layout文件。这样一来,不同类型的文件只需使用一个文件。

这样做是否会损失任何功能?

一个配置为使用_layout文件的_hosts文件示例。

@page "/"

@namespace BlazorApp1.Pages

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
    Layout = "_Layout";
}

<component type="typeof(App)" render-mode="ServerPrerendered" />
英文:

I am using Blazor with .NET Core 7. The out-of-the-box project comes with _hosts file. But cshtml files do not use _host but use _layouts. I have seen several projects pointing the _host file to a _layout file. This way there is only one file that can be used by the different file types.

Am I losing any functionality doing this?

An example of _hosts file configured to use _layout file.

@page "/"

@namespace BlazorApp1.Pages

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
    Layout = "_Layout";
}

<component type="typeof(App)" render-mode="ServerPrerendered" />

答案1

得分: 1

不,我不这么认为。

Net6.0 由于某种原因切换到了 _Host/_Layout 模式。Net7.0 又切换回了在 Net5.0 及之前版本中使用的单文件 _Host 文件模式。

英文:

No, I don't believe so.

Net6.0 switched to the _Host/_Layout pattern for some reason. Net7.0 switched back to the single file _Host file pattern used in Net5.0 and earlier releases.

答案2

得分: 0

_Host.cshtml是Blazor应用程序的根RazorPage,所有您的Razor组件都会呈现在它上面。它不是一个布局页面,因为它不包含@RenderBody()/@RenderSection()

默认模式与MrC,即Shaun Curtis的答案类似(在.NET 6中,它只指定了根应用程序组件(App.razor)的呈现位置。在.NET 7中,_layout.cshtml被删除,所有代码都移动到了_Host.cshtml中),但您可以为_host.cshtml指定一个布局页面,然后自己分离代码。

这个文档与Blazor App的项目结构有关,可能会有所帮助。

> _Host.cshtml:作为Razor Page实现的应用程序的根页面:当初始请求应用程序的任何页面时,将呈现此页面并返回响应。主机页面指定了根App组件(App.razor)的呈现位置。

> _Layout.cshtml:应用程序的根页面的布局页面。

英文:

_Host.cshtml is the root RazorPage of the Blazor application ,all your razor components would be rendered on it.It's not a layout page for it dosen't contain @RenderBody()/@RenderSection()

The default parttern is similar with MrC aka Shaun Curtis's answer(In .net 6,it just specifies where the root App component (App.razor) is rendered. In .net 7,_layout.cshtml is removed,all codes were moved to _Host.cshtml),but you could spcific a layout page for _host.cshtml and seperate the codes yourself

Blazor .NET Core 7要使用哪个布局文件? _host.cshtml和_layouts.cshtml。

This document related with project structure of Blazor App may help

> _Host.cshtml: The root page of the app implemented as a Razor Page: When any page of the app is initially requested, this page is rendered
> and returned in the response. The Host page specifies where the root
> App component (App.razor) is rendered.

> _Layout.cshtml: The layout page for the _Host.cshtml root page of the app.

huangapple
  • 本文由 发表于 2023年7月17日 18:51:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定