Friday, 28 March 2008

HttpHandler for all requests in ASP.Net on IIS

Setting up a HttpHandler for handling ALL incoming requests is straight forward with .Net and IIS. Provided you've created your .Net HttpHandler project to sucessfully handle the requests, you need to do the two following tasks:

1) Ensure that the web.config references the HttpHandler you've created

To the httpHandlers section of your web.config file add a reference to your httpHandler for all HTTP verbs and extensions:

<system.web>
   <httpHandlers>
      <add verb="*" path="*" type="your.assembly.reference"/>
   </httpHandlers>
</system.web>

2) Setup IIS to route all request to your handler

a) IIS 5 (Win XP)
  • Open the website properties in IIS
  • Select the Home tab
  • Click the Configuration button
  • In new window select the Mappings tab
  • Click the Add button
  • In new window's executable field Browse to the ASP.Net ISAPI dll (usually something like C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll)
  • In the Extension field type .*
  • Untick the Check that file exists box and click OK
N.B. There is a bug that if your OK button is not active you need to click the executable field text box, you should see the middle section of the text in the box change from /.../ to the full file path this should now make the OK button active.
b) IIS 6 (Windows Server 2003)
  • Open the website properties in IIS
  • Select the Home tab
  • Click the Configuration button
  • In new window select the Mappings tab
  • Insert an entry in to the Wildcard application maps with the ASP.Net ISAPI dll (usually something like C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll)
  • Untick the check that file exists box and click OK

You should now see all requests being handle via your handler. If you are having issues, a good place to start finding out why is your IIS logs - but where are my IIS logs?. Good luck!

7 comments:

jms said...

By All do you also mean requests directed to classic ASP pages also.

Neil kilbride said...

All resource requests regardless of the document or it's extension will be handled by the HttpHandler.

Does anyone still use classic ASP? ;)

Max said...

Hi,

when I want to set the extension to .*, I get an error saying "Wrong extension format.
I also tried * but didn't work.
I am in IIS 5.
I removed the "Verify that file exists"
I tried "Limit to: HEAD,GET,POST"
I tried "All verbs"

Any ideas why it says that ?

Neil kilbride said...

I'm running IIS 5.1 and it works fine. Do you have multiple wildcard entries? I seem to remember IIS doesn't like this!

Max said...

In fact, I said I had IIS 5, but it's 5.1

.* is the only wildcard I am trying to add.

It seems I am not the only one who's not able to do that.

http://forums.asp.net/t/1191637.aspx
and
http://www.eggheadcafe.com/forumarchives/inetserveriis/Jul2005/post23311496.asp

So if you have 5.1, and it works fine, there is something I'm missing.

Anonymous said...

Thank you for the tip about the IIS 5.1 bug - where you have to click back in the 'Executable textbox'. It has been annoying me the whole morning!!

Mayank P - Clarion, India said...

c) for IIS 7:
Refer this URL : http://learn.iis.net/page.aspx/508/wildcard-script-mapping-and-iis-7-integrated-pipeline/