Search code examples
asp.net-mvcasp.net-mvc-4pageload

Run code every time a page loads


So I've come to understand that MVC doesn't really have a forms PageLoadevent equivalency so where do I put a code that I would like to execute every time a page loads? I'd like to check for a cookie.


Solution

  • Put it in the Constructor of the MVC Controller.

    Or like this:

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
        // check for cookies!
    }