3

是否可以在 ASP.NET Web API 路由配置中添加一个路由,以允许处理看起来有点像文件名的 URL?

我尝试在 中添加以下条目WebApiConfig.Register(),但这不起作用(使用 URI api/foo/0de7ebfa-3a55-456a-bfb9-b658165df5f8/bar.group.json):

config.Routes.MapHttpRoute(
  name: "ContextGroupFile",
  routeTemplate: "api/foo/{id}/{filetag}.group.json",
  defaults: new { controller = "foo", action = "getgroup"}
  );

以下确实有效(它FooController.GetGroup(id,filetag)按预期调用)(使用 URI api/foo/0de7ebfa-3a55-456a-bfb9-b658165df5f8/group/bar):

config.Routes.MapHttpRoute(
  name: "ContextGroupFile",
  routeTemplate: "api/foo/{id}/group/{filetag}",
  defaults: new { controller = "foo", action = "getgroup"}
  );

失败的案例返回一个 IIS 错误(404 - 找不到文件),看起来它是由我的应用程序之外的东西创建的。错误页面(由 IIS Express 生成)包含以下错误详细信息:

Module = IIS Web Core
Notification = MapRequestHandler
Handler = StaticFile
Error Code = 0x80070002

我想这意味着一个叫做“StaticFile Handler”的东西在它到达我的代码之前就已经处理了它。最大的问题是:有没有办法防止这种情况发生?

4

2 回答 2

6

您可以在进行以下设置后尝试:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
于 2012-11-12T21:25:25.063 回答
1

替换处理程序路径过滤器。通常看起来像这样:

<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

尝试使用 'path="*"'

<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
于 2017-06-14T21:02:44.457 回答