44

I see in global.asax.cs from an ASP.NET MVC project

 protected void Application_BeginRequest()
 {
 }

but when I try to add this to my project, I don't see what is calling this method. I see that the base System.Web.HttpApplication has this event but I don't see anything overriding it or subscribing to this event.

Can someone explain how you wire up Application_BeginRequest in ASP.NET MVC?

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
leora
  • 188,729
  • 360
  • 878
  • 1,366

2 Answers2

110

I'm afraid Cos's answer isn't quite accurate. You don't have to wire it up because the base HttpApplication class does it for you. There isn't an interface or an override here; HttpApplication uses reflection to hook up these methods to the events based on the method name. So it's a bit of convention-based magic that has been in the framework for a while. It's very strange, and I think they only did it to maintain similarities with Classic ASP back in the day and/or provide a shortcut to avoid writing lots of small HttpModules.

For the curious, load up HttpApplication in Reflector and hunt for the HookupEventHandlersForApplicationAndModules() method. Or, load HttpApplicationFactory and look at the ReflectOnApplicationType() and ReflectOnMethodInfoIfItLooksLikeEventHandler() (love that name!) methods.

Rick Strahl discussed this on his blog a few years ago.

Is it something you really need to know to use ASP.NET? No, but knowing it certainly removes some of the mystery.

Nicholas Piasecki
  • 25,203
  • 5
  • 80
  • 91
  • There are a lot of magic methods like this in ASP.NET. Remember `Page_Load` and `Page_Init` in Web Forms? – Jalal Apr 08 '18 at 17:50
3

Any ASP.NET application is an object (or class) of type :

public class Global : System.Web.HttpApplication (you will find this in the global.asax)

The ASP.NET engine invoke by IIS creates an instance of your object and the HttpApplication interface demands Application_BeginRequest, which is invoke by IIS (by way of the ISAPI)

When the ASP.NET Engine creates an instance of your class it looks like this:

HttpApplication thisAspApp = new YourASPApplication()
thisApplication.Begin_Request()

Because it casts your app as a derived type, the known interface can be directly accessed without need for overrides. While HttpApplication is a class it is being used as an interface by way of casting. If you add a new method (or property) to your class the ASP.NET engine can not access that method because it is only aware of your application as a generic HttpApplication. In VS if you go to the global.asax and right click over HttpApplication in the class declaration and select "Go To Definition" (or press F12) you can see the structure of base class. (or you can find it in MSDN online).

Cos Callis
  • 5,051
  • 3
  • 30
  • 57
  • so wouldn't you need override here somewhere? – leora Jul 06 '11 at 01:25
  • nitpicker here, class and object aren't interchangeable words. ;) – John Farrell Jul 06 '11 at 03:51
  • @jfar, they are two different states. When referring to the 'application' (in this case, a 'web app') the descriptive code (_contained in global.asax_) is a 'class', once the application is instantiated in memory it is an object. Depending on in which state the reader envisions the app either the term object or class could be appropriate. – Cos Callis Jul 06 '11 at 04:07
  • @cos if its an implementation of interface don't we always have to implement this? – Muhammad Adeel Zahid Jul 06 '11 at 04:41
  • @Muhammad Yes. The global.asax _always_ inherits from System.Web.HttpApplication thereby implementing it's interface. – Cos Callis Jul 06 '11 at 11:25
  • but i haven't implemented this in global.asax and app is running fine. i m using asp.net mvc 3 – Muhammad Adeel Zahid Jul 06 '11 at 11:29
  • @Muhammed, I have not yet actually used MVC 3 (next project...) but a quick look about suggests that MVC 3 uses a class derived from HttpApplication, `public class MvcApplication : NinjectHttpApplication` I found this at: https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application – Cos Callis Jul 06 '11 at 12:42
  • 1
    -1. Read Nicholas Piasecki's answer, or this related question: http://stackoverflow.com/questions/21646605/global-asax-magic-functions – Sphinxxx Jun 26 '15 at 15:42
  • -1 Sorry, but that explanation isn't correct and is actually misleading regarding how the events from global asax gets wired up. Nicholoas Piasecki correctly answers the opening question. – De Shan Baptiste Jul 31 '15 at 20:31