0

I'm working on a web application in ASP.NET MVC. When the user logs in, I want to retrieve his navigation (varies between user accounts) and his information from the database, and keep the info between controllers.

I'm displaying the navigation in _Layout.cshtml so I'm wondering best practices to pull the navigation and other user info from the database and storing it between controllers. This information is only retrieved once and stored through-out the user "log-in session".

What is the ideal solution? Should I create a BaseController which other controllers inherit and put my logic in the constructor and put the info in a global ViewBag? Or should I use the Session object in the Login method? Or should I create a static method and call it directly from _Layout.cshtml?

What should I do? I want to do this globally once, so I don't have to do this in each controller action.

Gaui
  • 8,723
  • 16
  • 64
  • 91
  • 1
    Yes, you are in right way to my mind. I posted an answer here like your idea: http://stackoverflow.com/a/21883256/1534785 .Maybe it can help – Jeyhun Rahimov Apr 01 '14 at 09:34
  • Exactly what I was thinking. Then make a singleton so it doesn't retrieve the info from DB each time an action is executed and uses the in memory object. – Gaui Apr 01 '14 at 10:27
  • The BaseController and singleton is not working as I expected, because the controller only has a lifetime through the request, so the object is always null and recreated (pulled from the database). Is this something I could use? http://stackoverflow.com/questions/343899/how-to-cache-data-in-a-mvc-application – Gaui Apr 02 '14 at 09:04
  • No doubt, you should call db method in every request. If you want to call one time, you can use Session, Cookie etc. – Jeyhun Rahimov Apr 02 '14 at 09:32

0 Answers0