ASP.NET MVC Global Variables

How do you declare global variables in ASP.NET MVC?

+55
c # global-variables asp.net-mvc
Feb 25 2018-11-21T00:
source share
6 answers
public static class GlobalVariables { // readonly variable public static string Foo { get { return "foo"; } } // read-write variable public static string Bar { get { return HttpContext.Current.Application["Bar"] as string; } set { HttpContext.Current.Application["Bar"] = value; } } } 
+38
Feb 25 2018-11-15T00:
source share

Technically, any static variable or Property in a class anywhere in your project will be a global variable, for example.

 public static class MyGlobalVariables { public static string MyGlobalString { get; set; } } 

But, as @SLaks says, they can "potentially" be bad practice and dangerous if not handled correctly. For example, in the example above, you will have several requests (threads) trying to access the same property, which can be a problem, if it is a complex type or collection, you will have to implement some form of locking.

+75
Feb 25 '11 at 15:35
source share

You can put them in the application:

 Application["GlobalVar"] = 1234; 

They are global only within the current IIS / Virtual application. This means that on webfarm they are local to the server and inside the virtual directory, which is the root of the application.

+23
Feb 25 '11 at
source share

For non-static variables, I sorted them using the Application Class Dictionary , as shown below:

In Global.asax.ac:

 namespace MvcWebApplication { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { private string _licensefile; // the global private variable internal string LicenseFile // the global controlled variable { get { if (String.IsNullOrEmpty(_licensefile)) { string tempMylFile = Path.Combine(Path.GetDirectoryName(Assembly.GetAssembly(typeof(LDLL.License)).Location), "License.l"); if (!File.Exists(tempMylFile)) File.Copy(Server.MapPath("~/Content/license/License.l"), tempMylFile, true); _licensefile = tempMylFile; } return _licensefile; } } protected void Application_Start() { Application["LicenseFile"] = LicenseFile;// the global variable bed AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } } } 

And in the controller:

 namespace MvcWebApplication.Controllers { public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { return View(HttpContext.Application["LicenseFile"] as string); } } } 

That way we can have global variables in ASP.NET MVC :)

NOTE. If your object is not a string, simply write:

 return View(HttpContext.Application["X"] as yourType); 
+18
May 21 '12 at 7:25
source share

You can also use a static class like Config class or something like that ...

 public static class Config { public static readonly string SomeValue = "blah"; } 
+7
Feb 25 '11 at 2:53
source share

The steel is far from hot, but I combined @abatishchev's solution with the response from this post and got to this result. Hope this is helpful:

 public static class GlobalVars { private const string GlobalKey = "AllMyVars"; static GlobalVars() { Hashtable table = HttpContext.Current.Application[GlobalKey] as Hashtable; if (table == null) { table = new Hashtable(); HttpContext.Current.Application[GlobalKey] = table; } } public static Hashtable Vars { get { return HttpContext.Current.Application[GlobalKey] as Hashtable; } } public static IEnumerable<SomeClass> SomeCollection { get { return GetVar("SomeCollection") as IEnumerable<SomeClass>; } set { WriteVar("SomeCollection", value); } } internal static DateTime SomeDate { get { return (DateTime)GetVar("SomeDate"); } set { WriteVar("SomeDate", value); } } private static object GetVar(string varName) { if (Vars.ContainsKey(varName)) { return Vars[varName]; } return null; } private static void WriteVar(string varName, object value) { if (value == null) { if (Vars.ContainsKey(varName)) { Vars.Remove(varName); } return; } if (Vars[varName] == null) { Vars.Add(varName, value); } else { Vars[varName] = value; } } } 
0
Nov 13 '17 at 15:27
source share



All Articles