How do I load another _layout.cshtml depending on the role?

My application will have different roles, one role will be Global Administrator who will have options like adding users, adding companies, etc.

The template I bought has one _layout.cshtml, however I need it to load another one depending on the user's role.

One that has a completely different menu.

My viewstart

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

      

and my layouts.cshtml

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>INSPINIA | @ViewBag.Title</title>

    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css'>
    <!-- Add local styles, mostly for plugins css file -->
    @if (IsSectionDefined("Styles"))
        {@RenderSection("Styles", required: false)}

    <!-- Add jQuery Style direct - used for jQGrid plugin -->
    <link href="@Url.Content("~/Scripts/plugins/jquery-ui/jquery-ui.css")" rel="stylesheet" type="text/css" />

    <!-- Primary Inspinia style -->     
    @Styles.Render("~/font-awesome/css")                                                
    @Styles.Render("~/Content/css")
</head>
<body>

    <!-- Skin configuration box -->
    @Html.Partial("_SkinConfig")

    <!-- Wrapper-->
    <!-- PageClass give you ability to specify custom style for specific view based on action -->
    <div id="wrapper" class="@Html.PageClass()">

        <!-- Navigation -->
        @Html.Partial("_Navigation")

        <!-- Page wraper -->
        <div id="page-wrapper" class="gray-bg @ViewBag.SpecialClass">

            <!-- Top Navbar -->
            @Html.Partial("_TopNavbar")

            <!-- Main view  -->
            @RenderBody()

            <!-- Footer -->
            @Html.Partial("_Footer")

        </div>
        <!-- End page wrapper-->

        <!-- Right Sidebar -->
        @Html.Partial("_RightSidebar")

    </div>
    <!-- End wrapper-->

    <!-- Section for main scripts render -->
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @Scripts.Render("~/plugins/slimScroll")
    @Scripts.Render("~/bundles/inspinia")

    <!-- Skin config script - only for demo purpose-->
    @Scripts.Render("~/bundles/skinConfig")

    <!-- Handler for local scripts -->
    @RenderSection("scripts", required: false)
</body>
</html>

      

Everything works fine, but I need to create another layouts.cshtml with a link to another partial navigation view and this is where the question comes in.

How can I force the application to load that specific layouts.csthml when the user belongs to a specific role?

+3


source to share


4 answers


You can split the layout into two parts, one for general users and one for administrator. Then there are two or three types of ways to render these layouts, as outlined in this article , using _ViewStart.cshtml or Setting the layout in a view or via Action Result.



0


source


This answer assumes that the default SimpleMembership role manager is used in MVC4:

If it's only a specific section of the layout, you can use Razor. Place this where your menu code ... (which is probably in _Navigation):

@if (Roles.IsUserInRole("GlobalAdmin"))
{
    @Html.ActionLink("Admin only link", "ActionName", "ControllerName")
    @Html.ActionLink("Another admin link", "ActionName", "ControllerName")
}

      



If it's a specific action method, you can specify a layout:

string layoutName = Roles.IsUserInRole("GlobalAdmin") ? "_LayoutAdmin" : "_Layout";
return View(model, layoutName);

      

You can automate this last method using a custom actionresult, or maybe even an action filter.

+3


source


You can use Area. Implementing an abstract class AreaRegistration is a good start. In Global.asax Application_Start () registers all the scopes you have

AreaRegistration.RegisterAllAreas ()

Ask two MVC projects to say something Something.Admin and another Something.Web

This tutorial is good, but all in one project I like different projects http://www.codeproject.com/Articles/714356/Areas-in-ASP-NET-MVC

+1


source


One option is to use MvcSiteMapProvider

and enable protection to show / hide different menu options depending on who signed up. It is based on MVC AuthorizeAttribute

or any subclass, so it will support any combination of users, roles, or custom security requirements that you may have already defined.

If you prefer not to use a third party library, another option is to reverse engineer AuthorizeAttributeAclModule

to build your own system, which is MVC based AuthorizeAttribute

.

Full disclosure

I am a major contributor MvcSiteMapProvider

.

0


source







All Articles