Sunday 21 July 2013

Setting up HttpHandler in IIS 7 or IIS 8 integrated mode

I spent hours troubleshooting a 404 problem with it and it turned out to be one of those doh! moments when I finally figured it out. I thought it was worth posting something about it here because during my research into the matter I found a lot of scattered and contradicting information about it on the web. Some said you have to strongly name and sign your assembly and deploy to the GAC in order to use a custom httphandler with IIS, which is not true, and others had some downright way of going about it.

In any case it's very very simple. All you need to do is declare your handler under web.config under <system.webserver> like this:

<system.webserver>
 <add name="MyHandlerClass" path="MyHandlerMethod" verb="GET" type="FullNamespace.ClassName, FullNamespace.SyncServer" resourceType="Unspecified" preCondition="integratedMode" />
</system.webserver>

Notes:

1)You must declare this inside the <system.webserver> not <systerm.web> and in fact make sure you do not include a declation for the same handler unser <system.web> otherwise it'll get confused

2)the name must match the name of your HttpHandler class

3)the path is the method in your httphandler class that you want to call which is also part of the path when invoking it

4)type is just the full assembly name for your custom header including the class name for your custom httphandler

5)resourceType is the equivalent to "verify that file exists"in II 6. leaving it as undefined prevents IIS from checking for a file specifically which may be what you need particularly if your handler just maps to a path rather than serving a file

6)this tells IIS to run the handler in integrated mode

now the most important one, for me, anyway and what kept me troubleshooting this for hours:

7)If it's an MVC project... MAKE SURE TO ADD IT TO YOUR IGNORE ROUTES CONFIGURATION!

1 comment: