Reputation: 10824
In our ASP.NET Core application we have several roles, we want each role has its own layout, for doing this we came up with idea of having separate layout for each role:
~/Views/Shared/Layouts/_DefaultLayout.cshtml
~/Views/Shared/Layouts/_Role_1_Layout.cshtml
~/Views/Shared/Layouts/_Role_2_Layout.cshtml
~/Views/Shared/Layouts/_Role_3_Layout.cshtml
~/Views/Shared/Layouts/_Role_4_Layout.cshtml
For switching between these roles we modify the ~/Views/_ViewStart.cshtml
to this:
@{
if (this.User.IsInRole("Role1"))
{
Layout = "~/Views/Shared/Layouts/_Role_1_Layout.cshtml";
}
else if (this.User.IsInRole("Role2"))
{
Layout = "~/Views/Shared/Layouts/_Role_2_Layout.cshtml";
}
// ....
else
{
Layout = "~/Views/Shared/_DefaultLayout.cshtml";
}
}
this works in the first place, but when I log in with another user with for example Role1
, instead of switching the current layout to use _Role_1_Layout.cshtml
, it displays a blank page.
Any idea?
Upvotes: 2
Views: 2736
Reputation: 415
Simply set Layout to the name of the layout not to the path of the layout:
if (this.User.IsInRole("Role1"))
{
Layout = "_Role_1_Layout";
}
....
Upvotes: 0
Reputation: 7350
Looks like you fitting 'else', but there is a mistake in the path?
Layout = "~/Views/Shared/_DefaultLayout.cshtml";
Probably, should be:
Layout = "~/Views/Shared/Layouts/_DefaultLayout.cshtml";
I this is not a cause of your problem, could you share repo with your code or at least working example??
Upvotes: 1