ASP.NET MVC Actions that return different views, or just make a ton of Actions?

Posted by Nate Bross on Stack Overflow See other posts from Stack Overflow or by Nate Bross
Published on 2010-03-12T20:41:35Z Indexed on 2010/03/12 20:57 UTC
Read the original article Hit count: 290

So, I am in a situation, where I need to display a different view based on the "Role" that the authenticated user has.

I'm wondering which approach is best here:

[Authorize(Roles="Admin")]
public ActionResult AdminList(int? divID, int? subDivID) 
{
    var data = GetListItems(divID.Value, subDivID.Value);
    return View(data);
}

[Authorize(Roles = "Consultant")]
public ActionResult ConsultantList(int? divID, int? subDivID)
{
    var data = GetListItems(divID.Value, subDivID.Value);
    return View(data);
}            

or should I do something like this

[Authorize]
public ActionResult List(int? divID, int? subDivID)
{
    var data = GetListItems(divID.Value, subDivID.Value);
    if(HttpContenxt.User.IsInRole("Admin") 
    { return View("AdminList", data ); }

    if(HttpContenxt.User.IsInRole("Consultant") 
    { return View("ConsultantList", data ); }

    return View("NotFound");
}

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about web-development