【发布时间】:2016-02-16 21:23:36
【问题描述】:
我有一个 URL 以 /Plan/Production 开头的页面。我正在使用 HTML5 History API,当我点击某个链接时,它会将 URL 的开头更改为 /Plan/Selling。这在单击链接和使用前进/后退按钮时效果很好,但是当我在 URL 开始更改为 /Plan/Selling 后刷新页面时,它不再加载布局。我的解决方案是编辑我的 _ViewStart.cshtml 文件以检查 URL 的开头,并基于此呈现布局,如下面的代码 sn-p 所示。
问题是,如果我的 URL 以 /Plan/Selling 开头,它甚至不会使用 _ViewStart.cshtml 文件。仅当 URL 以 /Plan/Production 开头时,它才会使用此文件。添加console.logs 证实了这一点。需要明确的是,文件的位置是/Plan/Views/_ViewStart.cshtml。我使用的两个 URL 示例是 https://localhost:44301/Plan/Selling/DetailsPPVS/1(不加载布局,从不使用 ViewStart 文件)和 https://localhost:44301/Plan/Production(加载正确的布局并使用 ViewStart 文件)。
如何让https://localhost:44301/Plan/Selling/DetailsPPVS/1 使用/Plan/Views/_ViewStart.cshtml?
来自/Plan/Views/_ViewStart.cshtml的代码:
<script>console.log("Plan/Views/_ViewStart.cshtml");</script>
@{
if (Context.Request.Path.StartsWith("/Plan/Selling", StringComparison.OrdinalIgnoreCase))
{
<script>console.log("Pjax Layout");</script>
Layout = "~/Views/Shared/_PjaxLayout.cshtml";
}
else {
<script>console.log("Regular Layout (~/Areas/Plan/Views/Shared/_Layout.cshtml)");</script>
Layout = "~/Areas/Plan/Views/Shared/_Layout.cshtml";
}
}
【问题讨论】:
标签: javascript c# asp.net-mvc razor asp.net-core-mvc