tags:

views:

259

answers:

2

Is there a way to know the main/calling request in an httpmodule? I only want my module code to run for the main aspx page, not the images and scripts. I could check the path for ".aspx" but it seems like there should be a better way.

+3  A: 

From your module you could call HttpContext.Current.Handler and see if it is a Page. You'd probably have to do this in the PostMapRequestHandler event or a later event in the life cycle.

Alternatively, maybe you can check which HttpHandlerFactory is being used for the request. If it is the PageHandlerFactory, you can run your logic, and otherwise you can skip it.

William Gross
Checking CurrentHandler works and is ok if checked in the PreRequestHandler event. I am using spring.net so in my case it's the spring page handler. Checking that is similar to checking for the aspx extension but maybe this is a bit cleaner. Thanks
Marshal
A: 

IIS won't invoke the ASP.NET handler on images and scripts*, so a HTTPModule should only run on ASPX requests anyways.

  • Unless you mean scripts served by as an embedded resource, ie webresource.axd.
FlySwat
That is true for IIS6, but not for IIS7.
Greg
OP didn't clarify what version is he using. So the ASP.NET pipeline is invoked for every request? That seems silly.
FlySwat
Yes it's invoked for every request (aspx and otherwise). At least it is on the visual studio integrated web dev server.
Marshal
The Visual Studio integrated dev web server is nothing like IIS in production.
FlySwat
It needs to work for IIS6, IIS7 and dev web server. That is irrelevant to the question.
Marshal
Actually its very relevant, I just don't think you get it.
FlySwat
Please explain further.
Marshal