简体   繁体   中英

Same view using different master pages - ASP.NET MVC

I'm new to ASP.net MVC and I want to use a view but with a different Master Page depending on the user Role .

For now, i'm leaning to use one controller who return View1 if the user is in Role1 and View2 is in Role2 . View1 and View2 contains the same partial view inside to render the content that is share by both but have a different master page.

I want to know if it's a good practice or if you recommend another design. My solution seems a little bit complicated to do something simple. Maybe I am missing something

Thanks !

You could have a function which returns the master name based on the user role and then write a custom action filter which will execute after the action and set the corresponding master page based on the currently connected user role:

public class MasterChooserAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);
        var result = filterContext.Result as ViewResult;
        if (result != null)
        {
            var user = filterContext.HttpContext.User;
            result.MasterName = GetMaster(user);
        }
    }

    private string GetMaster(IPrincipal user)
    {
        // TODO: based on the current user roles return the proper master page
        throw new NotImplementedException();
    }
}

and then simply decorate your base controller with this action filter or if this is an ASP.NET MVC 3 application simply declare it as global filter .

There are various ways to select the master page each having their merits.

The simplest would probably be to return the Master page name using the controller View method

public ViewResult Index() {

  var masterName = getMasterPageNameForUser(); //get you master page/layout name here

  return View("Index", masterName, model);

}

However this will lead to some repetitive code so an alternative could be to create a custom IViewEngine and set the master name there. Scott Hanselman's post and this coder journal post will give you an idea of how to create a custom view engine. From there it's a mater of setting the master name.

What happens if you come up with a third role? Fourth role? Instead of putting that kind of logic in your controller, what if the master page displays different things depending on their role? You could hide whole chunks of <div> or whatnot in the master. Then, you've only got one place to change it whenever the role dependency changes. Is the master page going to be that different based upon the role?

In your controller do

this.ViewBag.Layout = something

in your view

Layout = this.ViewBag.Layout

Simply select the layout in your view. You can dynamically change @{Layout = XXX} in mvc 3. see: http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx

Your controller could check the roles, and assign the layout to use, which then is assigned to @Layout in your view, but you could just as well keep this code in your view to determine the layout to use, since after all it is 'view logic'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM