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()
{
...
}
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.
source
share