35

I am having problems trying to map an HttpHandler in the web.config.

This is the relevant config bit:

<httpHandlers>
  <add verb="*" path="*.hndlr" type="MyAssembly.MyHandler, MyAssembly" validate="false" />
</httpHandlers>

When I navigate to http://localhost/myApp/whatever.hndlr I am getting a server error 404 (not found).

It's the 1st time I am hooking up an HttpHandler so I might be missing something - any help appreciated!

UPDATE:

I managed to get it working using both answers so far - who's able to explain why it works gets the answer marked!

This is my config (won't work if you don't have both - I am running IIS7 in classic mode)

System.web:

<httpHandlers>
    <add verb="*" path="*MyHandler.hndlr" type="MyAssembly.MyAssemblyHandler, MyAssembly" validate="false"/>
</httpHandlers>

System.webserver:

<handlers>
    <add name="MyHandler" verb="*" path="*MyHandler.hndlr" type="MyAssembly.MyAssemblyHandler, MyAssembly" validate="false" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script"/>
</handlers>
JohnIdol
  • 48,899
  • 61
  • 158
  • 242
  • 1
    Further to your update, if you are using IIS6 antything you put in the System.webserver section will be ignored as this is used only in IIS7 Integrated Pipeline mode. THe only reason to have both would be to have a single web.config that is valid for IIS7 pipelined and calssic mode and IIS6. – Ben Robinson May 20 '10 at 16:22
  • sorry it's IIS7 - but does that change things? If I remove the bit in the system.webserver section it doesn't work (404 not found) and we are in classic mode for compatibility reasons. – JohnIdol May 20 '10 at 17:07
  • and if I comment out the one in system.web I get "Failed to Execute URL" - it works only with both for some funny reason! :) – JohnIdol May 20 '10 at 17:18
  • 1
    Your update was a LIFE SAVER ... many thanks! – David Perlman Feb 07 '12 at 19:47
  • `validate` is no longer allowed in `system.webServer` – Jamie May 17 '19 at 17:11

9 Answers9

30

Are you using IIS7, if so is the application pool running in classic or pipelined mode? If it is IIS7 in pipelined mode then the handler reference needs to go into the following section

<system.webServer>
    <handlers>
    </handlers>
<system.webServer>

rather than in the following section.

<system.web>
    <httpHandlers>
    </httpHandlers>
</system.web>
Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
  • tried (had to add name="MyHandler" to the attributes) - looks promising but getting a different error now --> HTTP Error 500.21 - Internal Server Error Handler "MyHandler" has a bad module "ManagedPipelineHandler" in its module list – JohnIdol May 18 '10 at 15:56
  • to answer your other question I am running in classic mode (not pipelined) - which explains why I am getting the error described in the comment above :) – JohnIdol May 18 '10 at 16:05
  • Yes the solution was a bit of a mixture between the two answers - I am about to post the solution and who's able to better explain what the heck is going on gets the points! :) – JohnIdol May 20 '10 at 15:22
11

Just as a guide for those stuck with this problem I found the crucial attribute to be..

resourceType="Unspecified"

I originally followed a Microsoft example to set this up and they had it as

resourceType="File"

which just kept giving me 404 errors. My HTTPHandler is returning graphics.

Hope this helps :)

CResults
  • 5,100
  • 1
  • 22
  • 28
5

i am using IIS7, the solution is:

in section

<system.web>
    <httpHandlers>
        <add verb="*" path="*.ashx" type="CVOS.MyDocumentHandler"/>
    </httpHandlers>
<system.web>

and section

<system.webServer>
    <handlers>
       <add name="pdfHandler" verb="*" path="*.ashx"   type="CVOS.MyDocumentHandler" /> 
    </handlers>
</system.webServer>
evilone
  • 22,410
  • 7
  • 80
  • 107
4

What is the extension of your handler? If you are using a custom extension like .hndlr you may also need to add a ScriptMap in IIS and point it to the ASP.NET runtime so that IIS can forward the request to the correct processor.


  1. In IIS7 go to your website
  2. Under the IIS group go to Handler Mappings
  3. Under Actions click Add Script Map
  4. Set Request Path to *.hndlr
  5. Set Path to the ASP.NET runtime (%windir%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll) or whatever version you are running.

Then in your web.config you will need to register the handler in the appropriate section as described in the other answer.

Wallace Breza
  • 4,898
  • 1
  • 28
  • 33
2

It is also possible to experience this error if you have set up the handler for 32 bit, but you are running in 64 bit (or vice versa). It's easy to set up both and have all the bases covered.

Note "preCondition", and "scriptProcessor" differences.

<handlers>
    <add name="MyHandler_32bit" verb="*" path="*MyHandler.hndlr" preCondition="bitness32" type="MyAssembly.MyAssemblyHandler, MyAssembly" validate="false" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" />
    <add name="MyHandler_64bit" verb="*" path="*MyHandler.hndlr" preCondition="bitness64" type="MyAssembly.MyAssemblyHandler, MyAssembly" validate="false" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" />
</handlers>
James
  • 2,823
  • 22
  • 17
2

None of the previous answers worked for me.
I'm using IIS 8.5, .Net v4.0, Integrated, and was still getting a 404 with the following handler config:

<system.webServer>
    <handlers>
       <add name="testEmail" path="*.em" verb="*" type="MyApp.testRazorEmailHandler, MyApp" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
</system.webServer>


I enabled tracing and found the following :

116. -HANDLER_CHANGED 

    OldHandlerName              testEmail 
    NewHandlerName              System.Web.Mvc.MvcHandler 
    NewHandlerModules           ManagedPipelineHandler 
    NewHandlerScriptProcessor
    NewHandlerType              System.Web.Mvc.MvcHandler, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 


As you can see it looks like it had correctly picked up the request using my custom HttpHandler testEmail but MVC had stolen it.
I opened my route definitions in RouteConfig.cs and found that adding:

   routes.IgnoreRoute("{resource}.em");

I got it to ignore requests meant for my Handler.
Hope this helps someone - I was tearing my hair out!

farmer-Bri
  • 143
  • 2
  • 6
1

Hopefully my solution will help others. On a server move from IIS6 to 7.5, both .Net 4.0 Integrated, I had a Captcha control that quit working. It turns out that removing this attribute preCondition="integratedMode,runtimeVersionv2.0" from the <add> node in <system.webserver><handlers> resolved the issue.

InbetweenWeekends
  • 1,405
  • 3
  • 23
  • 28
1

This seems to be an edge case, but I had a customer where our httpHandler used in our application did not work on any of their servers. The handler pointed to an .ashx page and it was called from JavaScript.

The handler mapping showed up in IIS, the handler factory was there, but I would get a 404 when the browser requested the ashx page associated with the handler. After many different attempts to fix we finally browsed to the file in IIS on the server and it specifically showed a 404.7 being returned with this message.

•Request filtering is configured for the Web server and the file extension for this request is explicitly denied.

•Verify the configuration/system.webServer/security/requestFiltering/fileExtensions settings in applicationhost.config and web.config.

If you get this then Request Filtering is enabled for the .ashx extension at either your app or site level. Go to the Request Filtering option in IIS at both your site and app level and verify that the extension is not blocked. There are two different ways Request Filtering can be configured.

The default seems to be that it explicitly blocks only file extensions that are configured in the list (blacklist). The other way it can be configured is that only files specifically configured as allowed in the list are let through (whitelist). This second option is how the customer had configured all of their Windows Servers by default and it turns out that the .ashx file extension was not in the list of allowed extensions.

Harv
  • 555
  • 6
  • 17
0

This is the first thread that appears when I look for a verb that responds with a 404. In my case the solution was a configuration of VS

Tools > Options > Web projects > [x] Use 64 bit version

Sorry, my VS is in spanish

hestellezg
  • 3,309
  • 3
  • 33
  • 37