This is a migrated thread and some comments may be shown as answers.

[Solved] Controller's constructor being called when references in a Menu Action() method.

1 Answer 48 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Shawn
Top achievements
Rank 2
Shawn asked on 28 May 2012, 05:16 PM
EDIT: The title of this post should read "referenced" instead of "references"

I have a menu setup in my application using the following overloads of the MenuBuilder...
.Items(menu => {
                        menu.Add().Text("Projects").Action("Index""ProjectListing");
                        menu.Add().Text("Reports").Action("Index""Reports");
                        menu.Add().Text("Methodologies").Action("Index""Methodology");
                        menu.Add().Text("Users").Action("Index""User");
                        menu.Add().Text("Roles").Action("Index""ClientRole");
                    })

Now I've found through logging that each of the controllers (2nd parameter of the above Action() methods) is actually instantiated on every render of the View that hosts the menu.  Is this correct behavior?  I'm just thinking it could lead to some inefficiencies if one happens to be doing data access or any other such operation in the constructor of those controllers.  Please advise.
Thanks for a great product.

1 Answer, 1 is accepted

Sort by
0
Accepted
Ashok
Top achievements
Rank 1
answered on 22 Jun 2012, 04:02 PM
If you don't wont your controllers to be instantiated user 'Url()' instead of 'Action()'.

In some cases it is better to use Action().
For example : if you have [Authenticate] attribute at top of your controller class declaration. And user is not logged-in then if you use Action() it will try to create instance and when it find that attribute then menu item will not be created. (in sort your menu's will be auto managed for logged-in user and anonymous users) 
Tags
Menu
Asked by
Shawn
Top achievements
Rank 2
Answers by
Ashok
Top achievements
Rank 1
Share this question
or