加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 根据用户角色的类型更改布局

发布时间:2020-12-16 03:59:48 所属栏目:asp.Net 来源:网络整理
导读:我是MVC的新手,我在堆栈上看过类似的帖子,但他们似乎没有帮我解决问题.我有两个角色Admin和User.当一个人登录时,我希望根据该人是否具有管理员权限或用户权限来更改布局. 我的管理员布局是_Layout.cshtml 我的用户布局是_LayoutUser.cshtml 我的登录控制器中
我是MVC的新手,我在堆栈上看过类似的帖子,但他们似乎没有帮我解决问题.我有两个角色Admin和User.当一个人登录时,我希望根据该人是否具有管理员权限或用户权限来更改布局.

我的管理员布局是_Layout.cshtml

我的用户布局是_LayoutUser.cshtml

我的登录控制器中是否有可以根据用户角色更改布局的内容?用户的角色在Login表中指定.

id     LoginStudentNumber    LoginPassword      Role
1           123456              123456          Admin
2           234567              234567          Admin
3           345678              345678          User
4           456789              456789          User

登录控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMS.DAL;
using RMS.Models;

namespace RMS.Controllers
{
    public class LoginController : Controller
    {
        RMSContext db = new RMSContext();


        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(Login model,int? LoginStudentNumber,string LoginPassword)
        {


            //RMS.Models.Login user = db.Logins.Find(LoginID);
            RMS.Models.Login user = db.Logins.FirstOrDefault(l => l.LoginStudentNumber == model.LoginStudentNumber);


            if (user == null)
            {
               ViewBag.InvalidID = "Invalid User Id";

                return View(model);
            }



            if ((user.LoginPassword.Equals(LoginPassword)) && (user.Role.Equals("Admin")))

            {

                return RedirectToAction("Index","Home");


            }

            if ((user.LoginPassword.Equals(LoginPassword)) && (user.Role.Equals("User")))
            {

                return RedirectToAction("IndexUser","Home");


            }


            ViewBag.InvalidPassword = "Invalid Password";
            return View(model);

        }
    }
}

_Layout.cshtml页面:

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>@ViewBag.Title - Wits RMS</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body onload=display_ct();>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a href="@Url.Action("Index","Home")">
                    <img src="~/Icons/Wits---Logo---Mono-Neg---Colloquial---Ext.png" style="height:45px; width:80px; margin-right:5px;" alt="" />
                </a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Researcher","Index","Researcher")</li>
                    <li>@Html.ActionLink("Submission","Submission")</li>
                    <li>@Html.ActionLink("Publication","Publication")</li>
                    <li>@Html.ActionLink("Faculty","Faculty")</li>
                    <li>@Html.ActionLink("School","School")</li>
                    <li>@Html.ActionLink("Contact","Contact","Home")</li>
                    <li>@Html.ActionLink("About","About","Home")</li>
                    <li>@Html.ActionLink("Reports","Reports")</li>
                    <li>@Html.ActionLink("Logout","Login","Login")</li>
                </ul>
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>            
            <script type="text/javascript">
                function display_c() {
                    var refresh = 1000; // Refresh rate in milli seconds
                    mytime = setTimeout('display_ct()',refresh)
                }

                function display_ct() {
                    var monthNames = ["January","February","March","April","May","June","July","August","September","October","November","December"
                    ];
                    var dayOfWeek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
                    var strcount
                    var x = new Date()
                    var x1 = dayOfWeek [x.getDay()] + "," + x.getDate() + " " + monthNames[x.getMonth()] + " " + x.getFullYear();
                    x1 = x1 + " " + ('0' + x.getHours()).slice(-2) + ":" + ('0' + x.getMinutes()).slice(-2) + ":" + ('0' + x.getSeconds()).slice(-2);
                    document.getElementById('ct').innerHTML = x1
                    tt = display_c();
                }
            </script>

            <p>&copy; @DateTime.Now.Year - Wits RMS <span style="float:right; top:20px;" id='ct'></span></p>                             
        </footer>
    </div>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts",required: false)
</body>
</html>

_LayoutUser.cshtml页面:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>@ViewBag.Title - Wits RMS</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body onload=display_ct();>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a href="@Url.Action("IndexUser","Home")">
                    <img src="~/Icons/Wits---Logo---Mono-Neg---Colloquial---Ext.png" style="height:45px; width:80px; margin-right:5px;" alt="" />
                </a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">                    
                    <li>@Html.ActionLink("Submission","IndexUser","Publication")</li>                    
                    <li>@Html.ActionLink("Contact","ContactUser","AboutUser","Home")</li>
                    <li>@Html.ActionLink("Logout","Login")</li>
                </ul>
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <script type="text/javascript">
                function display_c() {
                    var refresh = 1000; // Refresh rate in milli seconds
                    mytime = setTimeout('display_ct()',"Saturday"];
                    var strcount
                    var x = new Date()
                    var x1 = dayOfWeek[x.getDay()] + "," + x.getDate() + " " + monthNames[x.getMonth()] + " " + x.getFullYear();
                    x1 = x1 + " " + ('0' + x.getHours()).slice(-2) + ":" + ('0' + x.getMinutes()).slice(-2) + ":" + ('0' + x.getSeconds()).slice(-2);
                    document.getElementById('ct').innerHTML = x1
                    tt = display_c();
                }
            </script>

            <p>&copy; @DateTime.Now.Year - Wits RMS <span style="float:right; top:20px;" id='ct'></span></p>
        </footer>
    </div>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts",required: false)
</body>
</html>

解决方法

您只需要修改/Views/_ViewStart.cshtml文件即可.

@{
    if (this.User.IsInRole("Admin") || !this.User.Identity.IsAuthenticated) {
        Layout = "~/Views/Shared/_Layout.cshtml";
    } else {
        Layout = "~/Views/Shared/_LayoutUser.cshtml";
    }
}

You didn’t mention what should happen if no user is logged in,so I added the check and just sent them to the _Layout.cshtml view. You should change the logic as needed for your application.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读