2

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?

1
  • Are you hitting the appropriate Layout when switching? For example, if you set breakpoints on each of the Layout =… and you start switching roles, do you at least hit the appropriate Layout you expect to have? If this works, then you can at least eliminate this. If not, then this might help figure out a different angle to solve the issue. Oh and by switching…do you mean login/logoff and relogin? Or something else? Commented Jan 25, 2017 at 12:49

2 Answers 2

1

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??

Sign up to request clarification or add additional context in comments.

Comments

0

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";
    }

....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.