Mat*_*ete 4 razor asp.net-core asp.net-core-mvc-2.0
我正在从空站点模板构建新的 ASP.Net Core 2.0,并尝试让_ViewStart.cshtml页面正常工作。我已经创建了Views -> Shared -> _ViewStart.cshtml页面,其中包含以下代码:
@{
Layout = "~/Views/Shared/Layout/_Layout.cshtml";
}
Run Code Online (Sandbox Code Playgroud)
在我用于登录页面视图的Index.cshtl文件中,我有以下代码:
@{
ViewData["Title"] = "Index";
//Layout = "~/Views/Shared/Layout/_Layout.cshtml";
}
<h2>Index</h2>
Run Code Online (Sandbox Code Playgroud)
如果我在 Visual Studio 2017 中运行 F5 调试,索引页面会加载并仅显示“索引”,如果我查看源代码,布局中没有 HTML。如果我取消注释布局行并重新加载页面,一切都会正常。两个文件中的两个布局路径是相同的,那么为什么Index.cshtml文件不从_ViewStart.cshtml读取?
我不确定我是否只是错过了添加包或其他东西来使其工作,或者 ViewStart 在 .NET Core 中是否像在 .NET 4.5 中那样工作?
它的工作原理就像以前一样。这是将其放入错误文件夹的情况。
它(_ViewStart.cshtml)应该位于根视图文件夹Views/_ViewStart.cshtml中,而不是Views/Shared文件夹中
和通常
_ViewStart.cshtml都不_ViewImports.cshtml放在/Views/Shared文件夹中。这些文件的应用程序级版本应直接放置在该/Views文件夹中。
ASP.NET Core 中的参考布局:在每个视图之前运行代码
| 归档时间: |
|
| 查看次数: |
2801 次 |
| 最近记录: |