I’m implementing a simple HttpModule, where I want some code to run when the web application is started. But I’m surprised to find that the Application_Start event I would normally use from Global.asax is not available from a HttpModule. Is that correct, or am I missing something here?
How do I hook into the Application_Start event from an HttpModule?
Update:
I’ve come to simple solution using the Init event instead, but it still smells a bit funny to me.
The Application_Start method is called only one time during the life cycle of an application. You can use this method to perform startup tasks such as loading data into the cache and initializing static values. You should set only static data during application start.
An HttpModule is a component that is part of the ASP.NET request processing pipeline and is called on every request that is made to your application. Note that HttpModules can have access to the life cycle events of a request and hence they can be used to modify the response as well.
An HTTP module is an assembly that is called on every request that is made to your application. HTTP modules are called as part of the ASP.NET request pipeline and have access to life-cycle events throughout the request. HTTP modules let you examine incoming and outgoing requests and take action based on the request.
HttpModule are called before and after the request handler executes. They are intended to enable a developer to intercept, participate, or modify each request. There are 22 available events that can be subscribed to that enables the module to work on the request in various stages of the process.
I got interested in this thread and how a website started while fixing an bug in an old ASP.NET site.
So I put together a demo, to see how it would work. Seems the order IS from the web.config.
You can see here https://github.com/jradxl/MVC-Website-Without-Global.asax.cs It implements Robert Koritnik solution - thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With