Wednesday, February 17, 2010

Questions on Global.asax

1)What is main difference between Global.asax and Web.Config?
ASP.NET uses the global.asax to establish any global objects that your Web application uses. The .asax extension denotes an application file rather than .aspx for a page file. Each ASP.NET application can contain at most one global.asax file. The file is compiled on the first page hit to your Web application. ASP.NET is also configured so that any attempts to browse to the global.asax page directly are rejected. However, you can specify application-wide settings in the web.config file. The web.config is an XML-formatted text file that resides in the Web site’s root directory. Through Web.config you can specify settings like custom 404 error pages, authentication and authorization settings for the Web site, compilation options for the ASP.NET Web pages, if tracing should be enabled, etc.

2)What does Global.asax file do?
The Global.asax file is an optional file that contains code for responding to
application-level events raised by ASP.NET or by HTTP modules.

3)The Global.asax is code-inside with default.How to change Global.asax to code-behind?
In global.asax you need to modify the @ Application directive.
<%@ Application Codebehind="Global.asax.cs" Inherits="YourNamespace.Global" %>
Then create your .asax.cs file, and add a class named Global derived from System.Web.HttpApplication.

4)How is the global.asax file different from an ASP global.asa file? The global.asax file is very similar to the global.asa file in classic ASP."You can add a global.asax file to the root of each Web application on your site."The global.asax supports the familiar Application_Start, Application_End, Session_Start, and Session_End events."In addition, ASP.NET adds support for several new events. Please see Microsoft's .NET documentation for more information.

5)List the event handlers that can be included in Global.asax?
Application_Start,
Application_End,
Application_AcquireRequestState,
Application_AuthenticateRequest,
Application_AuthorizeRequest,
Application_BeginRequest,
Application_Disposed,
Application_EndRequest,
Application_Error,
Application_PostRequestHandlerExecute,
Application_PreRequestHandlerExecute,
Application_PreSendRequestContent,
Application_PreSendRequestHeaders,
Application_ReleaseRequestState,
Application_ResolveRequestCache,
Application_UpdateRequestCache,
Session_Start,
Session_End

You can optionally include "On" in any of method names. For example, you can name a BeginRequest event handler.Application_BeginRequest or Application_OnBeginRequest.You can also include event handlers in Global.asax for events fired by custom HTTP modules.Note that not all of the event handlers make sense for Web Services (they're designed for ASP.NET applications in general, whereas .NET XML Web Services are specialized instances of an ASP.NET app). For example, the Application_AuthenticateRequest and Application_AuthorizeRequest events are designed to be used with ASP.NET Forms authentication.

6)How can you use Global.asax to catch unhandled exceptions?Unhandled exceptions can be caught by using Application_Error() method of global.asax.
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception objErr = Server.GetLastError().GetBaseException();
string err = "Error in: " + Request.Url.ToString() +
". Error Message:" + objErr.Message.ToString();

}


A complete information on Global.asax can be found at : http://articles.techrepublic.com.com/5100-10878_11-5771721.html