gpt4 book ai didi

razor - ViewStart.cshtml 似乎没有从 View 中读取

转载 作者:行者123 更新时间:2023-12-04 01:56:08 25 4
gpt4 key购买 nike

我正在从空站点模板构建一个新的 ASP.Net Core 2.0,我正在尝试让 _ViewStart.cshtml 页面正常工作。我用以下代码创建了 Views -> Shared -> _ViewStart.cshtml 页面:

@{
Layout = "~/Views/Shared/Layout/_Layout.cshtml";
}

在我用于登录页面 View 的 Index.cshtl 文件中,我有以下代码:

@{
ViewData["Title"] = "Index";
//Layout = "~/Views/Shared/Layout/_Layout.cshtml";
}

<h2>Index</h2>

如果我在 Visual Studio 2017 中运行 F5 调试,索引页面会加载并只显示“索引”,如果我查看源代码,我的布局中没有 HTML。如果我取消注释布局行并重新加载页面,一切正常。两个文件中的两个布局路径相同,那么为什么 Index.cshtml 文件没有从 _ViewStart.cshtml 中读取?

我不确定我是否只是错过了添加包或其他东西来使这项工作正常进行,或者 ViewStart 在 .NET Core 中不能像在 .NET 4.5 中那样工作?

最佳答案

它像以前一样工作。这是将其放入错误文件夹的情况。

它 (_ViewStart.cshtml) 应该位于 Root View 文件夹 Views/_ViewStart.cshtml 而不是 Views/Shared 文件夹

Neither _ViewStart.cshtml nor _ViewImports.cshtml are typically placed in the /Views/Shared folder. The app-level versions of these files should be placed directly in the /Views folder.

引用 Layout in ASP.NET Core : Running Code Before Each View

关于razor - ViewStart.cshtml 似乎没有从 View 中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50553048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com