Daniel A. White
Daniel A. White

Reputation: 191037

Which View() to override in ASP.NET MVC Controller?

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?

Upvotes: 3

Views: 3010

Answers (2)

Craig Stuntz
Craig Stuntz

Reputation: 126587

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.

Upvotes: 3

Andrew Stanton-Nurse
Andrew Stanton-Nurse

Reputation: 6294

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

Upvotes: 9

Related Questions