title: MVC5 base controller that does setup of things that should always be done
body: <p>I'm working with an MVC5 project. In this project I have a few things I always want to do in pretty much every response to the client.</p>
  
  <p>For example, I always want to see if the user is logged in, and if so put the name of the user and their id into a the ViewBag variable for use in the .cshtml file.</p>
  
  <p>I have a base controller which all other controllers inherit from. My first thought was to do these things in the constructor of that controller, but this does not work as the <code>User</code> variable does not exist yet.</p>
  
  <p>Is there another way to do this, without calling a Setup() method in each Action?
  Can I listen to some event that fires before an ActionResult is returned and insert my ViewBag data there?</p>
  
  <p>Example of what does not work ;)</p>
  
  <pre><code>[InitializeSimpleMembership]
  public class BaseController : Controller
  {
      protected USDatabase _database = new USDatabase();
      public BaseController()
      {
          if (User.Identity.IsAuthenticated == true)
          {
              var usr = _database.UserProfiles.Where(x =&gt; x.UserName.ToLower() == User.Identity.Name.ToLower()).FirstOrDefault();
              if (usr != null)
              {
                  ViewBag.UserName = usr.UserName;
                  ViewBag.UserId = usr.Id;
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>My solution after reading the ideas in the answers below:</strong></p>
  
  <p>Created an Actionfilter I triggered on the base controller.</p>
  
  <pre><code>public class UserDataFilter : ActionFilterAttribute
  {
      //OnActionExecuting – This method is called before a controller action is executed.
      //OnActionExecuted – This method is called after a controller action is executed.
      //OnResultExecuting – This method is called before a controller action result is executed.
      //OnResultExecuted – This method is called after a controller action result is executed.
  
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          base.OnActionExecuting(filterContext);
  
          var User = filterContext.HttpContext.User;
  
          if (User.Identity.IsAuthenticated == true)
          {
              using (var db = new USDatabase()) { 
                  var usr = db.UserProfiles.Where(x =&gt; x.UserName.ToLower() == User.Identity.Name.ToLower()).FirstOrDefault();
                  if (usr != null)
                  {
                      var ViewBag = filterContext.Controller.ViewBag;
  
                      ViewBag.UserName = usr.UserName;
                      ViewBag.UserId = usr.Id;
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Base controller now looks like this: </p>
  
  <pre><code>[InitializeSimpleMembership]
  [UserDataFilter]
  public class BaseController : Controller
  {
      protected USDatabase _database = new USDatabase();
      public BaseController()
      {
  
      }
  }
  </code></pre>
  
  <p>And all my other controllers Implement the BaseController.</p>