How to define a global variable in web app

I have face a requirement,

I want client access a data center but without use database , so I want my web app can retain a global or Application session variable, that contains the data, every client can access the same data... I am try to declare in golabl, but seem it only can store String but others ...

how to solve this problem ?


To Share the data with all application users, you can use ASP.NET Application object. Given is the sample code to access Application object in ASP.NET:

Hashtable htblGlobalValues = null;

if (Application["GlobalValueKey"] != null)
    htblGlobalValues = Application["GlobalValueKey"] as Hashtable;
    htblGlobalValues = new Hashtable();

htblGlobalValues.Add("Key1", "Value1");
htblGlobalValues.Add("Key2", "Value2");

this.Application["GlobalValueKey"] = htblGlobalValues;

Application["GlobalValueKey"] can be used anywhere in the whole application by any user. It will be common to all application users.

ASP.NET Global Variables Example, However, you can make a global variable by creating a static class in a separate class file in your application. First, create a class called global in your application with the code given below. The class also has a static variable. Now, you can access it from anywhere in your forms after assigning a value to it. First, older versions of ASP used the Application [] object, but this is inefficient in ASP.NET. To get started in adding your static global variables in ASP.NET, create the App_Code folder. Then: Right-click on your project name and select "Add ASP.NET Folder" and then App_Code. Here is the code you can put in the class file.

Another option of defining a global variable is by creating a static class with a static property:

public static class GlobalVariables
    public static string MyGlobalVariable { get; set; }

You can make this more complex if you are going to use this as a data store, but the same idea goes. Say, you have a dictionary to store your global data, you could do something like this:

public static class GlobalData
    private static readonly object _syncRoot = new object();
    private static Dictionary<string, int> _data;

    public static int GetItemsByTag(string tag)
        lock (_syncRoot)
            if (_data == null)
                _data = LoadItemsByTag();

            return _data[tag];

    private static Dictionary<string, int> LoadItemsByTag()
        var result = new Dictionary<string, int>();

        // Load the data from e.g. an XML file into the result object.

        return result;

Global Variable In C#, While you can "get away with" using variables with the 'static' modifier to make a " global variable" or define methods as 'static' to get a 'global� I'm wanting to declare a variable as below for use in several pages on my site. I assume rather than declaring it once per page I can do it globally? I've tried it in a class (app_code folder) and in the global.asax page but my code can't seem to find it. I may be going abo9ut it all wrong though? Code as below

You can stuff data into the Application object if you want. It isn't persistent across application instances, but that may sufficient.

(I'm not for a minute going to suggest this is a best practice, but without a clearer picture of the requirements, that's all I can suggest.)

[Solved] How to define Global veriable in, How to declare a global variable value in c# || App Setting (key&value) in web config Duration: 3:39 Posted: Nov 27, 2018 If u want to define a global variable that will be accessible throughout the site u can define it in the Module, In 2.0 below is the procedure to add a module, Right click on ur website in solution explorer --> Add ASP.NET Folder --> App_Code. Then Right click on App_Code --> Add New Item --> Class

If you are using WebApplication or MVC just go to Global.asax (in WebSite project you need to add Global.asax from the add new item menu). I will explain to deploy two global variables for your web application: Open the Global.asax file, then define your variable in Application_Start function as following:

void Application_Start(object sender, EventArgs e)
   Application["variable1"] = "Some Value for variable1";
   Application["variable2"] = "Some Value for variable2";

If you want to use that those global variables in aspx pages just need to call them like this:

<p>I want to call variable1 <%=Application["variable1"].ToString() %></p>
<p>I want to call variable1 <%=Application["variable2"].ToString() %></p>

But if you want to use that those global variables in server-side call'em like this:

protected void Page_Load(object sender, EventArgs e)
   string str1 = Application["variable1"].ToString();
   string str2 = Application["variable2"].ToString();

Note: You must be aware that these global variables are public to all users and aren't suitable for authentication jobs.

How to declare a global variable value in c# || App Setting , NET there were different techniques for establishing global variables (aka module NET Core, what is the best practice for establishing such variables? You should create an application object, probably via middleware. Global.InvestigationRole thisRole = Global.InvestigationRole.Witness; // new instance of this enum. Buyer beware though. There are better ways of treating the concept of "global variables" in the .NET world, but the above will at least get you a layer of abstraction above repeating the same strings in all your ASPX pages.

You can also use Cache, which has advantages like ability to set expire time/date.

Best Practice for Global Variables?, I work on core 2.1 project web API i have more controller and i need to use static class variables are shared by the entire web application. library project. in that project create a static class, and define the values: In web applications, you can make a variable global by using the app state of the application. You can also declare variable in a session for the session level. But in a WinForms application, you can't use app or session states. However, you can make a global variable by creating a static class in a separate class file in your application.

How to make global variables on core 2 on level of all , C# Global Variables, Fields and Functions. How to define a static class to make " globals" accessible from anywhere. What is a "Global" Variable? By "forcing" yourself, or another developer, to activate a distinct NameSpace, and specify the Class to access the global variable, and by limiting the setting of the global variable to a specific procedure invoked in the static class, I'd argue that you are creating code that is self-documenting, and code in which global variable use is

C# How to use Global Variables, Fields and Functions, The most common way of accessing global variables in are by using Application, Below is a code example for usage of application object… ? to store the data specific to a user for the entire length of a user's visit to a website. When developing he maintains a well-defined engineering rules. Configuration ASP.NET 5 also comes with a new configuration framework. We will not go into too much detail of how this replaces app/web.config.But in general it is collection of string based key value pairs.

Global variable of, In this tutorial, I will discuss about global state in C# applications. NET, C#, Cloud or Web Developer looking for a resource covering New Technologies, in- depth Tutorials and Best This magazine covers ASP.NET In this scope, mutable variables are defined, mutated, and used inside a single method. In the previous section, you created a different configuration value for the development environment. You use the HostingEnvironment.EnvironmentName variable to dynamically determine which environment the app currently runs in. To learn more, see Use multiple environments in ASP.NET Core.

  • Are you referring to a session variable that will stay constant through the life of the session or a variable that will be shared between clients (other sessions)?
  • Can you share the example you're trying to work with?
  • See also…
  • Please stop adding signature blocks to your posts. They make it look like you're trying to spam your web site. There's a link to your profile on every post you make, which acts as your signature. You can add anything you like (within reason) to your profile page. That would be the place to post a link to your blog, web site, side projects, business, etc.
  • You do excess lookup to Application
  • -1 for no thread-safe locking. See @Pieter's answer for an example of what to add. Whether using Application or a static class, it's still necessary.
  • The static data is common only inside the pool, if some have many pools then they are different.
  • @Aristos - It is static within the AppDomain. Is that the same as within the pool?
  • @Pieter no the static is the same only inside the pool. If you have 2 pools then you have 2 different static value, and if a user, use both pools that is very possible, then the value is different. Make some tests to see it by your self. (you need iis6+)
  • @Aristos - That's very interesting. I'm curious then what's the difference between a pool and an AppDomain. Do you know some documentation of that?
  • @Pieter Look, the pool take care to run one AppDomain for one web site. You can have many web sites on the same pool, and you have many AppDomain for the same web site, each AppDomain in each pool. All that are not connected together.