tags:

views:

162

answers:

2

Which method View() is called last in the System.Web.Mvc.Controller? Or should I override each one? I would like to set some view data in my BaseController, which inherits from System.Web.Mvc.Controller.

Edit: I would like to store basic user information like IsLoggedIn and BasicUserDetails { PermissionLevel, UserName, EmailAddress, UserId }

Is this the place to set it?

+2  A: 

There are two different implementations (in RC 1):

    protected internal virtual ViewResult View(string viewName, string masterName, object model) {

    protected internal virtual ViewResult View(IView view, object model) {

All the others call these two. However, I would not presume that this is going to stay this way forever. I would have to imagine that future releases of the framework might change this.

Update: To store user info, use ASP.NET membership. It already tells you if the user is logged in -- Request.IsAuthenticated. You can store custom permissions using normal Membership functions. Email properties and the like are already supported. And of course the authentication providers are pluggable, so you can use whatever kind of authentication you want -- Windows, domains, OpenID, etc.

Update 2: Note also that ControllerBase has a virtual Initialize method you can override to set up stuff you'll need later in various actions.

Craig Stuntz
Thanks! This will help me alot!
Daniel A. White
I'm happy to help, but I wonder if this is the best place to set data. It's hard to tell without knowing more about what data you care to set in when you know the values, however.
Craig Stuntz
A: 

You'd probably want to override one of the "Filter" methods on controller. There are four such methods:

  • OnActionExecuting - Occurs before an action is executed
  • OnActionExecuted - Occurs after an action has been executed
  • OnResultExecuting - Occurs before the ActionResult returned by the Action is executed
  • OnResultExecuted - Occurs after the ActionResult returned by the Action has been executed

That's a better way to add functionality common to all actions, since it is officially supported and less likely to change

anurse