【问题标题】:Routing of file-like names in Asp.NET WebApi在 Asp.NET WebApi 中路由类似文件的名称
【发布时间】:2017-06-15 05:02:44
【问题描述】:

是否可以在 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”的东西在请求到达我的代码之前就已经掌握了它。最大的问题是:有没有办法防止这种情况发生?

【问题讨论】:

    标签: c# asp.net-web-api asp.net-web-api-routing


    【解决方案1】:

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

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
    

    【讨论】:

    • 不,那没有成功。但是,您的评论为我指明了一个有用的方向(我没有查看那些配置文件)。在深入研究了这些之后,我决定我试图做的事情似乎违背了 IIS 的设计,虽然我可能会以一种可能使它工作的方式来解决它,但重新设计我的应用程序可能会更好(并像我在原始帖子中的第二个示例一样使用请求 URI)
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      相关资源
      最近更新 更多