How can I get the SPWebApplication for the current request from Init method in a HttpModule?

3 Answers 3


Is SPContext.Current not yet available at this time? You should be able to get it from this. (SPContext.Current.Site.WebApplication from the top of my head)

Be careful though as this will call into the SharePoint object model for each incoming request, which will most likely hit the database as well. This may wreak havoc on performance and scalability.

  • The Init method is called only once per thread in the application pool. SPContext is null at this stage. In my code Init method calls another method OnStartup that does some initialization and does this only once per AppDomain. Mar 28, 2011 at 20:00

What about

var webApp = site.WebApplication;

I didn't have a reason to use this, so I don't know if it works, it's just an ideea. Try it, hope it helps

  • This can work but will hardcode the site collection URL. Is not an option even to read it from web.config file. Apr 1, 2011 at 18:14
 public void Init(HttpApplication application)
        //This is the first event that has SPContext populated for use
        application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);


Then in the application_PreRequestHandlerExceute you can access SPContext.Current.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.