2
Answers

What is Global.asax?

Photo of Mohammed FelFel

Mohammed FelFel

15y
4.2k
1
I want to make All Methods about Error handling in asp.net
some one device me at using global.asax
what is global.asax?
how can i use it ?
thanks , for help

Answers (2)

0
Photo of Andrew Fenster
NA 2.2k 1.5m 15y
Everything Mahesh said is correct.  I thought I'd throw in something about error handling, since that was what you were asking about.  Global.asax has a method called Application_Error.  It looks like this:

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
    }

This method fires whenever there's an unhandled exception.  So you can put code in this method to log the error, display an error message or do whatever else seems appropriate.  That being said, you really should try to catch and handle errors long before they get to the Global.asax page.  You can put try-catch blocks in individual methods.  You can catch them at the page level.  A very common technique is to write your own Page base class that catches errors and then make all your individual .aspx pages inherit from this base class.  That's what I would recommend.

Here is an article that describes how to make your own custom base Page class and use it to handle errors.  In most cases, this is the way to go.

http://www.codedigest.com/Articles/ASPNET/56_Using_Custom_BasePage_Class_in_ASPNet.aspx

Good luck to you.

0
Photo of Mahesh Chand
2 286.9k 123.7m 15y
From MSDN:

The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level events raised by ASP.NET or by HttpModules. The Global.asax file resides in the root directory of an ASP.NET-based application. At run time, Global.asax is parsed and compiled into a dynamically generated .NET Framework class derived from the HttpApplication base class. The Global.asax file itself is configured so that any direct URL request for it is automatically rejected; external users cannot download or view the code written within it.

The ASP.NET Global.asax file can coexist with the ASP Global.asax file. You can create a Global.asax file either in a WYSIWYG designer, in Notepad, or as a compiled class that you deploy in your application's \Bin directory as an assembly. However, in the latter case, you still need a Global.asax file that refers to the assembly.

The Global.asax file is optional. If you do not define the file, the ASP.NET page framework assumes that you have not defined any application or session event handlers.

Read here more about this

Here is an article on how to use Global.asax:

http://asp.dotnetheaven.com/aspnet/doc/applications/globalasax.aspx