mvcNewbie
mvcNewbie

Reputation: 520

MVC 4 Working With Global Data

I have a two part question and was hoping to get a "best practices" suggestion.

  1. As stated in the title, I am using MVC 4 to allow users to login to their organization. All of my models will store this org id to use when creating database entries. Because there are so many scenarios (user could set membership to stay logged in, so the login controller action would never trigger) or they could log off and log back in, etc. Is there one place that would be best to set this org info so it's accessible to all controllers/views?

  2. I currently just have a static variable set in one of my models, seems there should be a better, more robust way to do this.

Thank you everyone for taking time to help!

Upvotes: 0

Views: 283

Answers (3)

Erik Philips
Erik Philips

Reputation: 54628

Sounds like a job for Application State.

Application state is a data repository available to all classes in an ASP.NET application. Application state is stored in memory on the server and is faster than storing and retrieving information in a database. Unlike session state, which is specific to a single user session, application state applies to all users and sessions. Therefore, application state is a useful place to store small amounts of often-used data that does not change from one user to another.

No point re-inventing a wheel IMHO.

Upvotes: 1

Parv Sharma
Parv Sharma

Reputation: 12705

According to me you should keep the data about the user in the session storage/ cookie storage so that anybody can access it

everywhere you need to access it just check wether the user is authenticated and if true then access the data

Upvotes: 0

phil soady
phil soady

Reputation: 11328

You can create a BaseController Class which inturn inherits from : Controller.

So all of you controllers are declared

 public class HomeController : MYBaseController

This Base Controller class can do all the global work.

Upvotes: 0

Related Questions