Redirect from Global ASAX

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

I have some critical initialisations in constructor of the Global class
(Exception handling system, log system, db connection string)
Upon failure of any initiliazation methods, the web app should be redirected
to fatal error page.
Is there any way to redirect the request in the constructor of Global.aspx
file?

Thanks,
Ali
 
Hi Ali,

Thanks for posting in the community!
From your description, you put some initialization code in your
Application's Global class's constructor and you'd like to redirect the
current user to a custom error page if any error occurs during the
initialization, yes?

As for this problem, here are my suggestions:

1. Are the "initialzaiton" in the constructor for the whole appliation when
the application is started? If so, I think the Application_Start Event of
the Global class is the proper place for you to do the initialize
operations, this event is only fired when the whole application is started
(first time requested). And you call the
HttpContext.Current.Response.Redirect to redirect the current request to
any other url you like.

2. I don't think the constructor of the Global class(derived from
HttpApplication) is the correct place to do initialization. In ASP.NET
every request will pass through a series of processes which is called
pipeline. In the pipeline, the ASP.NET runtime will retireve a
HttpApplication class's instance(Global object) to fulfill the request. The
retrieving operations is done by the HttpApplicationFactory, here is the
description in MSDN:
----------------------------------------------------
The behavior of the application factory class can be outlined as follows:

The factory class maintains a pool of HttpApplication objects and uses them
to service requests for the application. The pool has the same duration of
the application's lifetime.
When the first request for the application arrives, the factory class
extracts information about the type of the application (the global.asax
class), sets up file monitoring for changes, creates the application state,
and fires the Application_OnStart event.
The factory picks up an HttpApplication instance from the pool and charges
it with the request to process. If no objects are available, a new
HttpApplication object is created. The creation of an HttpApplication
object entails the compilation of the global.asax application file.
The HttpApplication begins processing the request and won't be available
for new requests until the request completes. Should new requests for the
same resource come in, they will be handled by other objects in the pool.
The application object gives all registered HTTP modules a chance to
preprocess the request and figures out what type of handler can best handle
the request. It does this by looking at the extension of the URL requested
and the information in the configuration file.
----------------------------------------------------

So we can see that the HttpApplication(Global) instance is not identical,
there may be many instances in the pool and we can't expect when a new
instance will be constructed or which one will be retrieved from the pool
to deal with the current request. Do you think so? That's why I recommend
that we put the initalzation operation in the Application_Start event.

In addition, here are some other tech articles on the asp.net pipeline and
HttpApplication class:

#HttpApplication Class
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebhttpapplica
tionclasstopic.asp?frame=true

#The ASP.NET HTTP Runtime
http://msdn.microsoft.com/library/en-us/dnaspp/html/dngrfTheASPNETHTTPRuntim
e.asp?frame=true

Hope they are also helpful to you.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top