因此,我一直在尝试将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/{a},它只返回一个404。
我确信我只是“做错了(tm)”,但任何帮助都将不胜感激。
注意:我还尝试了其他几种配置,包括使用标记、创建一个/api文件夹以及使用全通配符将web.config添加到该文件夹。
发布于 2011-05-10 19:30:42
URLRoutingModule-4.0是在nancy处理程序之前列出的catch all处理程序。因此,它会在你的操控者被击中之前进场。您可以删除处理程序,添加您的处理程序,然后将它们添加回来,如下所示:
<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>您可以在Handler Mappings下的IIS7中看到处理程序的顺序,选择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“内容时通常做的事情。我还没有发现如何绕过对seconds 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/处理程序列表现在如下所示:

可以肯定的是,/Content/中的任何东西都将通过StaticFileModule提供服务。这里的诀窍似乎是指定:inheritInChildApplications="false"。
发布于 2013-04-04 12:04:02
很简单。只需输入路径,没有通配符。
<handlers>
<add name="Nancy" path="api" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" allowPathInfo="true" />
</handlers>这将匹配:
/api/{anything}
发布于 2011-05-10 23:43:18
似乎UrlRoutingModule-4.0的麻烦比它的价值更大。相反,我只是告诉MVC3忽略这些路由。这不是一个完美的解决方案,但在我找到更好的解决方案之前,我将不得不在RegisterRoutes中坚持这样做
routes.IgnoreRoute("api/{*route}");https://stackoverflow.com/questions/5942452
复制相似问题