在Asp.NET的WebAPI类文件名路由 [英] Routing of file-like names in Asp.NET WebApi

查看:150
本文介绍了在Asp.NET的WebAPI类文件名路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to add a route in the ASP.NET Web API routing configuration that allows handling URLs that look a bit like file names?

我尝试添加在 WebApiConfig.Register以下条目(),但没有工作(使用URI API /富/ 0de7ebfa- 3a55-456a-bfb9-b658165df5f8 / bar.group.json

I tried adding the following entry in WebApiConfig.Register(), but that didn't work (using 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 /富/ 0de7ebfa- 3a55-456a-bfb9-b658165df5f8 /组/条

The following did work though (it called FooController.GetGroup(id,filetag) as expected) (using 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前preSS产生)包含以下错误信息:

The failed case returns an IIS error (404 - file not found) that looks like it was created by something outside my application. The error page (generated by IIS Express) contained the following error details:

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

我想这意味着一种叫做StaticFile处理程序得到了它手中的请求时,它达到了我的code之前。最大的问题是:有没有办法prevent是什么?

I guess that means something called a "StaticFile Handler" got its hands on the request before it reached my code. The big question is: is there a way to prevent that?

推荐答案

你可以有以下设置后尝试:

Can you try after having the following settings:

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

这篇关于在Asp.NET的WebAPI类文件名路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆