gpt4 book ai didi

c# - 从登录用户单击页脚菜单时如何隐藏登录布局

转载 作者:太空宇宙 更新时间:2023-11-03 21:00:01 24 4
gpt4 key购买 nike

“已登录用户”和“未登录用户”我有不同的布局,如下所示。

@if (!Request.IsAuthenticated)
{
Layout = "~/Views/Shared/_Layout.cshtml";

}
else if (User.IsInRole("Candidate"))
{
Layout = "~/Views/Shared/_LoggedInLayout.cshtml";
}
else if (User.IsInRole("Referrer"))
{
Layout = "~/Views/Shared/_ReferrerLayout.cshtml";
}

现在,当用户登录并访问任何页面时,我会显示一个 Left Nav Bar Partial View with some 5-6 menu items

enter image description here

另外,就像其他网站一样,我也将页脚设为 “AboutUs”“Terms and Condition”

问题:

现在,当登录用户单击任何页脚选项时,即使在这种情况下,也会显示左侧导航栏菜单项。它被显示的原因是因为用户已登录。但是如果用户单击页脚菜单项,我不想显示任何左导航栏菜单选项。假设登录用户单击页脚中的“关于我们”,我希望它的行为方式与未登录用户单击时相同。

预期行为:整个页面的呈现方式应与未登录用户单击时的呈现方式相同。不应显示左侧导航栏菜单项。示例网站:I want a behaviour just like this web site

当前行为:

enter image description here

预期行为:

enter image description here

我的 About.cshtml

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

This is About us page.

最佳答案

在 View 中指定布局将覆盖任何设置,比如 Views/_ViewStart.cshtml 文件。所以,这应该是您所需要的:

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

<h2>Index</h2>

This is About us page.

覆盖整个 View 文件夹的 View

如果特定 Controller 的所有 View 都使用特定 View ,您还可以将 _ViewStart.cshtml 文件添加到该 Controller 的 Views 文件夹中。

例如,如果您有一个 FooController,其 View 有一个 Views/Foo 文件夹,您可以添加一个 _ViewStart.cshtml那里的文件让所有这些 View 使用 _FooLayout.cshtml (或其他)。其他一切仍将使用您的“全局”_ViewStart.cshtml 文件指示的任何内容。

如果您向 MVC 项目添加一个区域,您可以看到它的实际应用;模板化代码将在该区域内添加一个 _ViewStart,因为它更本地化,所以将覆盖非区域 _ViewStart。

关于c# - 从登录用户单击页脚菜单时如何隐藏登录布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46356833/

24 4 0