ASP.NET MVC: Ensure Session Variable is Always Populated

In a recent project, I needed to ensure that a session variable was populated for every View. Using ASP.NET WebForms one could perform a check for the session variable in the Page_Load event of a master page but this obviously doesn’t work for the MVC Framework as there is no such page life cycle. Despite the lack of a “page life cycle” like WebForms, there is still a request life cycle in which a number of events are fired before and after the controller action.

To solve our problem we need to do two things:

  1. Create a base controller from which all other controllers will be derived
  2. Override the OnActionExecuting event in the base controller

In the following code I have done both the named items above. I have created a class named BaseController that extends the Controller class and have overridden the OnActionExecuting event which will fire each time an action method is requested. Within this event, you simply check the session variable you need populated and if it does not have a value, populate it.

public class BaseController : Controller
    protected override void OnActionExecuting(ActionExecutingContext filterContext)

        // If the current user is authenticated, check to make sure the user's
        // profile has been loaded into session
        if (User.Identity.IsAuthenticated && HttpContext.Session["UserProfile"] == null)
            Session["UserProfile"] = UserProfile.GetUserProfile(User.Identity.Name, true);

Now when generating our Controllers for the application, instead of extending the Controller class, just extend the BaseController class we created above.

public class HomeController: BaseController
    public ActionResult Index()
        // Since we are extending from our custom BaseController class
        // the OnActionExecuting event will be fired and the session variable
        // will be populated
        return View();

You can also simply override the OnActionExecuting event for a single Controller if the logic doesn’t need to span multiple Controllers but be care not to duplicate code if doing this.

4 Responses to “ASP.NET MVC: Ensure Session Variable is Always Populated”

  1. Brian B Says:

    Cool post. I’m bookmarking this b/c I’m going to need Session access on all my pages in the near future. Thanks.

  2. Raghu Says:

    A great idea, thanks for sharing.

  3. Petra Says:

    What if I need it in my web api?

  4. Petra Says:

    I need to assign the session a value from my Web API because it is where I am authenticating the user.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: