CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Adding CodeBehind for Global.asax file in asp.net 2.0

By Satheesh Babu
Posted On Feb 24,2008
Article Rating:
Average Rating: 3.5
No of Ratings: 2
No of Comments: 3
Category: ASP.Net
Print this article.

Adding CodeBehind for Global.asax file in asp.net 2.0

Today, I was working on providing authorization for my project. To proceed this we planned to define all the available roles in Appsettings of the Web.config file (Role name can be changed in future and using AppSettings will prevent the need to change the code in future). Since we need all the available roles defined in web.config across the application for checking it with the user, we planned to read it once from web.config and keep it in a static read-only variable in Global.asax file. This prevents hitting web.config whenever needed. Doing this gives the access to all the available roles elsewhere in the application without hitting config file.

 

Since we are using ASP.Net 2.0 there is no Global.asax file in visual studio solution by default. I have added a new Global.asax file from add new item dialog in solution explorer. Unfortunately, it doesn’t follow codebehind model as in 1.x days. Below is the actual global.asax file added by visual studio 2005.

 

<%@ Application Language="C#" %>

<script runat="server">

 

    void Application_Start(object sender, EventArgs e)

    {

        // Code that runs on application startup

 

    }

   

    void Application_End(object sender, EventArgs e)

    {

        //  Code that runs on application shutdown

 

    }

       

    void Application_Error(object sender, EventArgs e)

    {

        // Code that runs when an unhandled error occurs

 

    }

 

    void Session_Start(object sender, EventArgs e)

    {

        // Code that runs when a new session is started

 

    }

 

    void Session_End(object sender, EventArgs e)

    {

        // Code that runs when a session ends.

        // Note: The Session_End event is raised only when the sessionstate mode

        // is set to InProc in the Web.config file. If session mode is set to StateServer

        // or SQLServer, the event is not raised.

 

    }      

</script>

 

Since this file is without class name I was unable to access the static variable declared in the above file else where in the application. After googling I found out a work around for this problem.

 




Workaround:

 

We need to explicitly add a new class file with the name Global.cs and inherit it with System.Web.HttpApplication [As the Global.asax codebehind file in 1.x].Now copy the whole content inside the <script> tag inside the class. So the resultant class looks like,

 

/// <summary>

/// Summary description for Global

/// </summary>

public class Global : System.Web.HttpApplication

{

    public static readonly string AdminRole = ConfigurationManager.AppSettings["ADM"];

    public static readonly string AnonymousRole = ConfigurationManager.AppSettings["ANM"];

 

       public Global()

       {

              //

              // TODO: Add constructor logic here

              //

       }

   

    void Application_Start(object sender, EventArgs e)

    {

        // Code that runs on application startup

 

    }

 

    void Application_End(object sender, EventArgs e)

    {

        //  Code that runs on application shutdown

 

    }

 

    void Application_Error(object sender, EventArgs e)

    {

        // Code that runs when an unhandled error occurs

 

    }

 

    void Session_Start(object sender, EventArgs e)

    {

        // Code that runs when a new session is started

 

    }

 

    void Session_End(object sender, EventArgs e)

    {

        // Code that runs when a session ends.

        // Note: The Session_End event is raised only when the sessionstate mode

        // is set to InProc in the Web.config file. If session mode is set to StateServer

        // or SQLServer, the event is not raised.

 

    }

}

 

And then we need add the inherits attribute to @Application directive in Global.asax file to point to the added class.So the resultant Global.asax file will contain,

 

<%@ Application Language="C#" Inherits="Global"%>

 

Now we have Global.asax file and Code behind file Global.asax.cs with a class name. By doing this we can go ahead and access the static variable any where in application. Below figure shows the static variable in visual studio intellisense.

 

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
Needs CodeFile
In VS 2008 I had to change the asax to look like this:

<%@ Application Language="C#" CodeFile="Global.cs" Inherits="Global" %>
Thank you...
Thanks to your effort!
Need using System
In the file "Global.cs" that is in the folder "App_Code", one needs the line... using System; ...at the very top of the file. HTH. Thank you. -- Mark Kamoski