When the base controller initializes to MVC

I am working on a web application in MVC 2 that uses a base controller for all other controllers to reduce controller initialization in one place. The base controller contains properties such as menu, current user, etc. Here is the code:

public class BaseController : Controller 
{
    private Common.MenuHierarchy _menu;
    private User _currentUser;

    internal NavMenuViewModel Menu(string pageId)
    {
        bool isGuest = this.CurrentUser.GroupProfileId == Constant.SecurityGroupProfile.Public;

        if (this._menu == null)
        {
            this._menu = CreateMenu();
        }
    }

    internal User CurrentUser
    {
        get
        {
            if (this._currentUser == null)
            {
                this._currentUser = CreateUser();
            }

            return this._currentUser;
        }
    }
}

All other controllers are obtained from BaseController. For instance,

public class HomeController : BaseController
{
   ...
}

public ActionResult Index()
{
   // Display home page
   ...
}

I noticed that after accessing the Home index, the _currentUser property was initialized. But if I set a point break inside get, this will not stop. I wonder when the base controller is initialized. Thank.

+4
source share
2 answers

Home , CTOR _currentUser, HomeController CTOR. , , , CurrentUser. .

+1

, HomeController. BaseClass . BaseController, HomeController. BaseController, . , BaseController HomeController.

UPDATE:

, , :

System.Diagnostics.Debug.WriteLine("This will be displayed in output window");

, , . , CurrentUser.

+1

All Articles