views:

200

answers:

3

I am kind of new to ASP.NET MVC, so need your help getting me through a problem:

In my application the LogOn will be done using the Role of the user. I have my custom database schema (like User, Role, UserInRole etc.) and I am using my custom MembershipProvider and RoleProvider to achieve Logon.

BTW I am using the MVC default Account controller itself with some modification. What I am trying to achieve now is that depending upon the Role of logged in user I want to create a different View for the user.

Can I use the returnUrl parameter of LogOn action method in any way? (This parameter is set to null by default). If yes how can I construct and send returnUrl depending on Role? Or is there any easier way to achieve this?

A: 

You decide what view to use (render) in the action - its not dictated directly by the URL

In the code you have access to the user object and from that can determine the roles that user is in - from that it should be straightforward enough to choose the view accordingly.

Murph
+1  A: 

By default, in your controller methods which return an ActionResult, if you just return "View()" then the page that will be displayed is the page in your Views directory with the name of your controller method. However, you can specify the name of the View which will be returned, or pass on the call to another controller method to return the appropriate view.

As in the following contrived example:

    public ActionResult Edit(string id)
    {
    // get the object we want to edit
        IObjectDefinition definedObject = _objectManager.GetObjectById(id);
        if (definedObject  != null)
        {
            ViewData.Add("definition", definedObject );// add to view data collection so can display on page

            IUser user = GetCurrentUser();// get from session/cookie/whatever
            if (!user.IsAccountAdmin)
 { 
                return View("Detail");// a readonly page as has no rights to edit
 }
 else
 {
  return View();// same object displayed in editable mode in another view
 }
        }
        else
        {
            return New();// call to controller method below to allow user to create new record
        }
    }


    public ActionResult New()
    {
        return View();
    }
Mike
A: 

I think you can do a RedirectToAction on successful login and since you now have a valid login user, in the action method you can use IsInRole property of the of the LoggedIn User and render an appropriate partial view.

Gboyega S.