Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic Event Wiring in Global.asax

I'm wondering if there's a way to automatically stub in the Global.asax's event handlers? Thus far, I've not been able to find any examples of how to do this. Seems I have to just find the list of delegate names available to me and type them in manually.

Intellisense doesn't seem to lend any useful info on the subject either.

like image 984
TheHolyTerrah Avatar asked Mar 17 '09 20:03

TheHolyTerrah


People also ask

What are the events in global ASAX file?

In this article, we learnt that Global. asax is a file used to declare application-level events and objects. The file is responsible for handling higher-level application events such as Application_Start, Application_End, Session_Start, Session_End, and so on.

What is Application_BeginRequest event in the global ASAX?

Application_BeginRequest: Fired when an application request is received. It is the first event fired for a request, which is often a page request (URL) that a user enters. Application_EndRequest: The last event fired for an application request.

Which event handler can be included in global ASAX?

Application_Error() – fired when an error occurs. Session_End() – fired when the session of a user ends. Application_End() – fired when the web application ends. Application_Disposed() - fired when the web application is destroyed.


2 Answers

The ASP.Net runtime uses reflection to dynamically look for methods with names like "Application_Start", "Session_Start" etc. and then binds them to the corresponding events on the HttpApplication class. You can effectively bind to any of the HttpApplication events simply by including a method in Global.asax.cs whose name is "Application_" followed by the name of the event. For example, to utilize the EndRequest event, add this to your Global.asax.cs file:

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        // Your code here
    }

See this Blog Entry from Rick Strahl for a bunch of helpful information on how this is done.

like image 55
JoshL Avatar answered Oct 07 '22 07:10

JoshL


All of the events of the HttpApplication class can have a handler in the global.asax.

like image 2
Robert C. Barth Avatar answered Oct 07 '22 06:10

Robert C. Barth