ASP.NET MVC Global Variables

Beginner picture Beginner · Feb 25, 2011 · Viewed 192k times · Source

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

Answer

Sunday Ironfoot picture Sunday Ironfoot · Feb 25, 2011

Technically any static variable or Property on a class, anywhere in your project, will be a Global variable e.g.

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 instance, in that above example, you would have multiple requests (threads) trying to access the same Property, which could be an issue if it was a complex type or a collection, you would have to implement some form of locking.