12

I need to write a HttpHandler that will serve up JavaScript files which are embedded resources in .DLLs in my project. A reference in a view cannot directly see such a resource, so I planned to use a HttpHandler module that would intercept any request with a path /js/[file] , find a matching embedded file and return the script.

The problem is that my HttpHandler code is never called, despite trying lots of different settings in the section of web.config. I'm obviously missing something but with no error messages I cannot see what that is. All I ever get is a 404 from the static file handler.

Q1) Am I missing something obvious?

Q2) Is there a way to get IIS to tell me why it is not calling my handler?

Summary: I am testing on IIS Express (v8) for an ASP.NET MVC 4 application.

I created a simple library that implements IHttpHandler and added a reference to this in my test MVC application, and the following lines in web.config:

  <system.webServer>
    <validation validateIntegratedModeConfiguration="true" />
    <handlers>
      <add name="ejs" path="js/*" verb="*" type="EmbeddedJsHandler.EmbeddedJsHandler, EmbeddedJsHandler" preCondition="integratedMode" />

The library is there, but it is never called. Any request with /js/test.js or whatever just results in a 404 error.

So far I've tried lots of different configurations and settings in the handler code. I've tried preCondition, resourceType="Unspecified", modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll"

I've tried paths:

  js/*.js
  js/* 
  js/*.*

I've checked the integrated mode settings section (in system.webServer) is being used, and confirmed it is.

I've searched stack overflow for similar cases, and tried many of the possible solutions.. still no joy.

Heck even Jon Skeet has these sort of problems! Why isn't my IHttpHandler being called?

Community
  • 1
  • 1
Quango
  • 12,338
  • 6
  • 48
  • 83

3 Answers3

28

Finally figured it out by accident - it was a missing routes.IgnoreRoute() in the RouteConfig.cs file - the MVC routing engine wasn't configured to ignore this path, so was passing it to the static file handler.

Doh!

Quango
  • 12,338
  • 6
  • 48
  • 83
  • 4
    I would have liked to see the line(s) you added to RouteConfig.cs for this. Any chance you still have it around? – Late Starter Dec 11 '13 at 22:59
  • 2
    Thanks, this sorted my issue. @ToboldHornblower here is the line in my RouteConfig.cs - routes.IgnoreRoute("PaymentCallback.ashx"); – ProNotion Sep 17 '14 at 15:14
  • 2
    Thank you! Lost 4 damn hours on this!!! But finally came across your solution. Thank you a 100 times! :) – AlexRebula Oct 08 '14 at 09:22
5

Check this:

How to: Register HTTP Handlers:

To register an HTTP handler for IIS 7.0 running in Integrated Mode:

  1. Compile the HTTP handler class and copy the resulting assembly to the Bin folder under the application's root folder.

  2. In the application's Web.config file, create a handlers element in the system.webServer section.

The following example shows how to register an HTTP handler that responds to requests for the SampleHandler.new resource. The handler is defined as the class SampleHandler in the assembly SampleHandlerAssembly.

<configuration>
  <system.webServer>
    <handlers>
      <add name="SampleHandler" verb="*" 
        path="SampleHandler.new" 
        type="SampleHandler, SampleHandlerAssembly" 
        resourceType="Unspecified" />
    </handlers>
  </system.webServer>
</configuration>

Note: The resourceType attribute performs the same function as the Verify file exists option in IIS manager for IIS 6.0.

For IIS 7.0 running in Integrated mode, only the registration in the handlers element is required.

Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
  • Thanks for the input.. I have read that article and my code is already in a separate DLL so it's compiled okay. – Quango Dec 20 '12 at 14:56
1

I cannot tell you directly why your handler isn't working, but I will give you an example of a handler we use and works for us:

<system.webServer>
    <handlers>
        <add name="JS handler" path="*.js" verb="*" type="Handlers.Minifiers.JSMinify" resourceType="Unspecified" preCondition="integratedMode" />
    </handlers>
</system.webServer>

We also have this segment, which is at least necessary for running in Cassini

<system.web>
    <httpHandlers>
        <add verb="*" path="*.js" type="Handlers.Minifiers.JSMinify" validate="false"/>
    </httpHandlers>
</system.web>

If this doesn't help, have tou tried using path="/js/*"?

Robert Fricke
  • 3,637
  • 21
  • 34
  • Now figured it out.. it's nothing to do with the web.config at all, it was a misconfigured routes.ignore setting.. (see my own answer). Thanks for the suggestion though! – Quango Dec 20 '12 at 15:01