Is there a way to subscribe to the HttpApplication’s BeginRequest event from Global.asax?
What I want to do is add a line of code to the Application_Start method of Global.asax which will then subscribe an object I have to the BeginRequest and EndRequest methods.
I know this can be done through HttpModules, but I can’t use HttpModules for various reasons that I will not go in to.
The aim is to be able to add the same line of code to multiple websites’ global.asax pages.
I’ve done some more digging, and have found out that the Init method is the way to go for me. I’m not convinved that the Global.asax constructor or the Application_Start method are the way to go. According to the MS docs:
I’m interpreting that as: “Don’t register an event here because it won’t be available after the first request.”
I’m interpreting that as: “Register your event here, because it will be registered for every new instance of the application, and the handler will not be dropped”.
The key here is “after all modules have been created”. The constructor is called before this, so won’t be the right place. As for Application_Start, I’ve tried many times, but I cannot get the event registration to stick – which is proven by the docs.
So I tried adding the code to the Init method and it worked just fine
i.e.