25

So I've been trying to map an http module to a sub-path of an MVC3 site. It should be pretty simple as I understand it, but it has not been working. The module is setup like so:

<handlers>
  <add name="Nancy" path="api/*" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" allowPathInfo="true" />
</handlers>

A matching section is also there for iis6 so I can run it under webdev.webserver. However testing both deploying to my local iis7 (under Win7) and with webdev.webserver, only /api actually calls the handler. If I call /api/{anything} it just returns a 404.

I'm sure I'm just "doing it wrong (tm)" but any help would be appreciated.

Note: I've also tried a couple other configurations including using a tag and creating a /api folder and adding a web.config to that folder with a full wildcard.

Martijn Laarman
  • 13,476
  • 44
  • 63
Chris Nicola
  • 14,384
  • 6
  • 47
  • 61

3 Answers3

20

Simple. Just put the path, no wildcard.

<handlers>
  <add name="Nancy" path="api" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" allowPathInfo="true" />
</handlers>

This will match:

/api/{anything}

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
  • Interesting, I don't do .NET work much anymore so I may not have time to verify this. If anyone else can though I'd gladly mark it as the correct answer. I have to be honest, that seems almost... too easy. I have to believe I would have tried that. – Chris Nicola Apr 05 '13 at 01:25
  • 1
    @ChrisNicola I use this for all my HttpHandlers for API. Works perfectly. – ThinkingStiff Apr 05 '13 at 02:45
  • 2
    This is the best answer in my opinion. – JDC Jun 13 '13 at 13:52
  • This doesn't work with the static file handler for some reason. This handler requires the approach shown in the accepted answer. – Roman Starkov Aug 18 '14 at 19:51
  • 1
    This _did_ work for me - serving jpgs from one handler (that I resize/stream dynamically) and js/png/etc from a second handler. – Bill Gillingham Feb 02 '15 at 14:52
  • Hey @BillGillingham, I'm currently having trouble serving jpgs from a controller--could you possibly post your web.config and routing code so I can see what I'm doing wrong? – Trevor Mar 13 '15 at 18:49
  • @threed - under system.webServer I have: – Bill Gillingham Mar 14 '15 at 20:08
  • This is such a basic solution, but took me hours to find. Thanks a bunch. – Quintonn Apr 25 '18 at 08:01
14

The URLRoutingModule-4.0 is a catch all handler listed before your nancy handler. It will thus come into play before your handler is ever hit. You can remove the handlers add yours and add them back in like so:

<handlers>
    <remove name="BlockViewHandler" />
    <remove name="UrlRoutingModule-4.0" />
    <add verb="*" path="robots.txt" name="robots" type="System.Web.StaticFileHandler"/>
    ... custom handlers here
    <add name="Nancy" path="api/*" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" allowPathInfo="true" />
    ... now add back UrlRoutingModule and BlockViewHandler
    <add path="*" verb="*" name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" preCondition="managedHandler" />
    <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> 
</handlers>

You can see the handler order in IIS7 under Handler Mappings select View Ordered List and it will list the order in which it loads the handlers top (first) to bottom (last).

You might need a second Web.config in your /api folder

<?xml version="1.0"?>
<configuration>
    <system.web>
      <httpHandlers>
        <clear />
        <add name="Nancy" path="*" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" allowPathInfo="true" />
      </httpHandlers>
    </system.web>
</configuration>

Similarly, this is what I usually do for "/static" content on websites. I have not found out how to circumvent the need for the seconds web.config.

EDIT

I had a hard time figuring this out when i had to as well and it seems my memory hasnt served me well. I dont specify a path/* handler anywhere instead I have this:

(only specifying simple wildcards/fully qualified paths to go around UrlRouting)

<location path="." inheritInChildApplications="false">
    <system.webServer>
        <!--
        ml: in .NET 4.0 its now safe to remove  from the modules section.
        Make sure you have a *. mapping to a ExtensionLessUrl hanlder in IIS
        this should improve performance a tad albeit neglectable.

        see: http://blogs.msdn.com/b/tmarq/archive/2010/04/01/asp-net-4-0-enables-routing-of-extensionless-urls-without-impacting-static-requests.aspx
        -->

        <validation validateIntegratedModeConfiguration="false" />
        <modules runAllManagedModulesForAllRequests="false" />
        <handlers>
            <remove name="BlockViewHandler" />
            <remove name="UrlRoutingModule-4.0" />
            <add verb="*" path="robots.txt" name="robots" type="System.Web.StaticFileHandler"/>
            .. Some company handlers i can't list 
            <add path="*" verb="*" name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" preCondition="managedHandler" />
            <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
        </handlers>
    </system.webServer>
</location>

Then in my /Content/web.config file I set the following:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <clear />
            <add name="StaticFiles" path="*" verb="*" modules="StaticFileModule" resourceType="Either" requireAccess="None" />
        </handlers>
        <staticContent>
            <clientCache cacheControlMaxAge ="31.00:00:00" cacheControlMode="UseMaxAge" />
        </staticContent>
    </system.webServer>
</configuration>

My handler list for /Content/ looks like this now:

Handler list

Which is about as sure as I can be anything in /Content/ will be served through StaticFileModule. The trick here seems to be specifying: inheritInChildApplications="false".

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
Martijn Laarman
  • 13,476
  • 44
  • 63
  • I've tried a number of combinations of that and it doesn't seem to work under IIS6 or IIS7. Same result. I've tried that again under under a second Web.config in the /api folder and in the main web.config – Chris Nicola May 10 '11 at 15:09
  • Very close to what I needed. But as it is named UrlRoutingModule-4.0 is a module not a handler. I've edited your answer with the correct web.config that worked for me. Thanks for your help! – Chris Nicola May 10 '11 at 15:15
  • Actually even with those changes this doesn't quite work. I fiddled around with it and I could only get it to work by disable MVC routing completely which wasn't ideal. You say you've gotten this working for static files routing, can you please double check what you did in your web.configs. – Chris Nicola May 10 '11 at 15:44
  • Ok, well since it's working for you I'll tentatively mark it as the answer. I'm ok with my solution below for now and it's just to crazy to play around with this problem any longer. Thanks so much for your help though – Chris Nicola May 11 '11 at 20:30
  • GOOD MAN. I've tried for a few hours in this. The idea of different web.config on the sub directories is perfect. Thanks!! Seems functionally equivalent to the adsutil.vbs SET /W3SVC/105364569/root/Content/ScriptMaps "" for IIS6 – penderi Oct 12 '11 at 13:55
  • A web.config in a separate folder did it for me, I didn't need to use the inheritInChildApplications trick, just clear all handlers in the folder's wbe.config – Chris S May 01 '12 at 09:28
9

Seems the UrlRoutingModule-4.0 is more trouble than it is worth. Instead I've just told MVC3 to ignore the routes. Not a perfect solution but until I have something that works better I'll have to stick with this in RegisterRoutes:

routes.IgnoreRoute("api/{*route}");
Chris Nicola
  • 14,384
  • 6
  • 47
  • 61