加载不同的母版页为不同的用户 [英] Load different master pages for different users

查看:93
本文介绍了加载不同的母版页为不同的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web项目(ASP.NET)我需要2个不同的母版页。一个用户喜欢的管理,以及一个用于通常的用户。我在哪里可以说明哪些母版页加载?我怎样才能加载正确的母版页,这取决于用户?

In my Web-project (ASP.NET) I need 2 different master pages. One for users like "Admin", and one for the usual users. Where can I indicate what master page to load? How can I load the correct master page, depending on the user?

推荐答案

当您的管理员用户尝试登录检查与数据库的用户名和密码,如果登录凭据有效,设置会话变量来表示这是一个admin会话。然后你就可以拥有它通过检查会话值来告诉你当前用户是否是管理员或普通用户返回假假真真的方法。

When your admin user try to log in check the username and password with the database and if the login credentials are valid, Set a session variable to indicate this is an admin session. Then you can have a method which returns true of false by checking the session value to tell you whether the current user is an admin or normal user.

当管理员登录是全成,设置此会话变量

When admin login is successfull, set this session variable

 Session["adminUserName"]=txtUserName.Text;

然后写一个方法来检查当前用户是否是管理员或不

Then write a method to check whether the current user is an admin or not

  public bool IsAdmin()
  {
    if(Session["adminUserName"]!=null)
    {
        return true;
    }
    else
    {
        return false;
    }
  }

在一个共同的地方这种方法(如您的基类左右)和页面生命周期过程中检查并加载相应的母版页。

Have this method in a common place (like your base class or so ) and check during the page life cycle and load the appropriate master page.

   void BasePage_PreInit(object sender, EventArgs e)
    {
        if(IsAdmin())
        {
           MasterPageFile = "~/MasterAdmin.master";
        }
        else
        {
             MasterPageFile = "~/MasterNormal.master";
        }
    }

如果它的ASP.NET MVC应用程序,你可以在你ActionMethod检查。

If Its an ASP.NET MVC application, You can check this in your ActionMethod.

public ActionResult Index()
{
 if(IsAdmin())
 {
   return View("Index", "MasterAdmin");
 }
 else
 {
   return View("Index", "MasterNormal");
 }
}

这篇关于加载不同的母版页为不同的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆