Web Application Setting in ASP.NET: Global.asax

0
45

Storing the settings of your ASP.NET application in the Global.asax file is the easiest way. This solution is based on using the Application_Start method and global property Application.
The Application property of HttpContext class returns HttpApplicationState object for the current HTTP request. HttpApplicationState object allows sharing of global parameters between multiple sessions and requests with you ASP.NET application.

A single instance of an HttpApplicationState class is created the first time a client requests any URL resource from within a particular ASP.NET application virtual directory. A separate single instance is created for each ASP.NET application on a Web server. A reference to each instance is then exposed via the intrinsic HttpContext.Application object.

Method Application_Start occurs as the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request.

Review the following example:

Thus when your web application starts the Application object will contain the values of your parameters and you can access them on page within your application.

Let’s consider the advantages and disadvantages of using Global.asax as storage for web application parameters.
Advantages:

Simplicity of realization
You can store the critical parameters, which you do not want to be changed without your confirmation (Developer Name, License Information). The changing of any parameter within Global.asax file requires recompilation of whole application. So it will be impossible to do without an access to source code
Disadvantages:

To change the value of any parameter it is required to recompile whole application. Therefore this method is not good for parameters, which have to be changed frequently.
Uploading of recompiled version will cause restart of whole application.

Source

Leave a Reply