So I have a Session variable which is set during the first user login
Session["ClientID"]
Basically this is used for theming (so the ClientID sets the theme/brand to appear on a website). Looking at the code applying
(Guid)Session["ClientID"]
All over the place just seems dirty and error prone, what the best design pattern to use to get the variable globally recognized. So I can do
this.CurrentClientID
or something similar on all MVC Pages. In theory I could overload the Controller class with a Custom class providing this ID, but I’m not sure how I would expose this to the View as well.
Any pointers to the best solution would be gratefully received!
No idea what you mean globally, but an extension method to the
ControllerBaseclass would render it accessible in all your controllers:and now inside each controller you can access it:
And if you want it to be even more globally available make it an extension method to the HttpContextBase class:
now everywhere you have access to the HttpContext (which is pretty much everywhere in an ASP.NET application) you simply use the extension method. For example inside a view: