英文:
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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论