Unable to map an HttpHandler to a "path/*" wildcard mapping
Asked Answered
M

3

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.

Militarist answered 9/5, 2011 at 21:0 Comment(0)
B
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".

Bookrack answered 10/5, 2011 at 11:30 Comment(6)
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 <location> under a second Web.config in the /api folder and in the main web.configMilitarist
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!Militarist
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.Militarist
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 thoughMilitarist
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 IIS6Jenn
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.configAparicio
S
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}

Shaer answered 4/4, 2013 at 4:4 Comment(8)
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.Militarist
@ChrisNicola I use this for all my HttpHandlers for API. Works perfectly.Shaer
This is the best answer in my opinion.Digest
This doesn't work with the static file handler for some reason. This handler requires the approach shown in the accepted answer.Beaver
This did work for me - serving jpgs from one handler (that I resize/stream dynamically) and js/png/etc from a second handler.Ethanethane
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?Mitre
@threed - under system.webServer I have: <handlers> <remove name="ImageHandler"/> <add name="StaticHandler" path="content" verb="" type="staticHandler" resourceType="Unspecified" preCondition="integratedMode"/> <add name="artDealer" path=".jpg" verb="*" type="ArtDealer" resourceType="Unspecified" preCondition="integratedMode"/> </handlers>Ethanethane
This is such a basic solution, but took me hours to find. Thanks a bunch.Actinoid
B
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".

Bookrack answered 10/5, 2011 at 11:30 Comment(6)
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 <location> under a second Web.config in the /api folder and in the main web.configMilitarist
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!Militarist
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.Militarist
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 thoughMilitarist
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 IIS6Jenn
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.configAparicio
M
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}");
Militarist answered 10/5, 2011 at 15:43 Comment(3)
Seems to be working nicely. Have you found any downsides doing it this way?Caricaria
None so far, been using it in production for quite a while now.Militarist
you might also want to add routes.IgnoreRoute("_Nancy/{*route}"); if you are using nancy v0.10+ to enable diagnostics.Orabelle

© 2022 - 2024 — McMap. All rights reserved.