Question

I am trying to configure a custom IHttpHandler in web.config and I don't understand the behavior I'm getting. I'm trying this using IIS express.

This is how my Sytem.WebServer section of web.config looks like:

   <system.webServer>
    <modules runAllManagedModulesForAllRequests="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <add name="dynamicPngHandler" verb="*" path="*.png" type="ImagServingTest.HttpHandler.PureCustomPngHandler, ImagServingTest"/>
    </handlers>
  </system.webServer>

http ://localhost:48705/default.png (does not work, I get a 404)

http ://localhost:48705/something/default.png (does not work, I get a 404)

http ://localhost:48705/something/something/default.png (does not work, I get a 404)

http ://localhost:48705/something/something/something/default.png (works)

I don't understand why this is happening. Why does the mapping fail until I have a url with three parts after localhost? I imagine the fact that this is an MVC4 app is important (although I can't see why).

UPDATE

I've tried this using web forms instead of MVC and it works as expected.

In the MVC project I tried moving the custom handler so it was the first on the handlers' list in web.config and it still did not work.

Was it helpful?

Solution

The reason for the request failing for those urls is that they were being covered by MVC's default routing rule: "{controller}/{action}/{id}" because all those have defaults.

I've added routes.IgnoreRoute("{ignore}.png"); to RouteConfig and I'm now seeing the behavior that I expected

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top