无法将 HttpHandler 映射到“path/*”通配符映射

发布于 2024-11-05 17:18:14 字数 508 浏览 1 评论 0原文

所以我一直在尝试将 http 模块映射到 MVC3 站点的子路径。按我的理解应该很简单,但它并没有起作用。该模块的设置如下:

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

iis6 也有一个匹配部分,因此我可以在 webdev.webserver 下运行它。然而,测试部署到我的本地iis7(在Win7下)和使用webdev.webserver,只有/api实际调用处理程序。如果我调用 /api/{anything} 它只会返回 404。

我确信我只是“做错了(tm)”,但任何帮助将不胜感激。

注意:我还尝试了其他一些配置,包括使用标签、创建 /api 文件夹以及使用完整通配符将 web.config 添加到该文件夹​​。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆流 2024-11-12 17:18:14

简单的。只需输入路径,不使用通配符。

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

这将匹配:

/api/{任何东西}

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}

街道布景 2024-11-12 17:18:14

URLRoutingModule-4.0 是一个在 nancy 处理程序之前列出的捕获所有处理程序。因此,它会在你的处理者被击中之前发挥作用。您可以删除添加您的处理程序并将它们添加回来,如下所示:

<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>

您可以在 IIS7 中的Handler Mappings下查看处理程序顺序,选择View Ordered List,它将列出顺序其中它从上(第一个)到下(最后)加载处理程序。

您可能需要在 /api 文件夹中添加第二个 Web.config

<?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>

类似地,这就是我通常对网站上的“/static”内容所做的操作。我还没有找到如何避免对秒 web.config 的需要。

编辑

当我不得不这样做时,我也很难弄清楚这一点,而且似乎我的记忆力不太好。我没有在任何地方指定 path/* 处理程序,而是这样:(

仅指定简单的通配符/完全限定的路径来绕过 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>

然后在我的 /Content/web.config 文件我设置了以下内容:

<?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>

/Content/ 的处理程序列表现在如下所示:

Handler list

这几乎可以肯定 /Content/ 中的任何内容都将通过 StaticFileModule 提供。这里的技巧似乎是指定:inheritInChildApplications="false"

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".

无需解释 2024-11-12 17:18:14

看来 UrlRoutingModule-4.0 的麻烦比它的价值还要多。相反,我只是告诉 MVC3 忽略路由。这不是一个完美的解决方案,但在我有更好的解决方案之前,我必须在 RegisterRoutes 中坚持使用它:

routes.IgnoreRoute("api/{*route}");

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}");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文