IIS8下的Web API嵌套资源失败

时间:2013-04-18 23:14:40

标签: asp.net iis asp.net-web-api iis-8

我有一个相当直接的Web API项目,在WebApiConfig.cs的默认路由下有以下路由:

config.Routes.MapHttpRoute(
   name: "ChildApi",
   routeTemplate: "api/{parentController}/{parentId}/{controller}/{id}",
   defaults: new { id = RouteParameter.Optional }
);

这在VS2012开发服务器下运行良好,导航到我的测试URI(/api/team/1/entries/2013-04-19)会返回正确的结果。一旦我发布了这个并尝试使用IIS8访问相同的URI,我收到以下404错误:

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

Most likely causes:
The directory or file specified does not exist on the Web server.
The URL contains a typographical error.
A custom filter or module, such as URLScan, restricts access to the file.

Things you can try:
Create the content on the Web server.
Review the browser URL.
Create a tracing rule to track failed requests for this HTTP status code and see which module is calling SetStatus. For more information about creating a tracing rule for failed requests, click here.

Detailed Error Information:
Module     IIS Web Core
Notification       MapRequestHandler
Handler    StaticFile
Error Code     0x80070002

所有非嵌套路由(例如/api/team/1)都可以正常工作,但似乎这个特定路由被解释为静态文件而IIS正在尝试提供它?有没有其他人见过/解决过这个问题?

1 个答案:

答案 0 :(得分:1)

好的,所以这很令人尴尬......

我在WebApiConfig.cs中设置了我的路线,如下所示:

config.Routes.MapHttpRoute(
   name: "ChildApi",
   routeTemplate: "api/{parentController}/{parentId}/{controller}/{id}",
   defaults: new { id = RouteParameter.Optional }
);

然后我继续在应用程序名称“api”下将我发布的项目添加到IIS。这(现在很明显)意味着我应该使用的URL是/api/api/team/1/entries/2013-04-19

按照以下方式更改我的路线,现在解决了我的问题:

config.Routes.MapHttpRoute(
   name: "ChildApi",
   routeTemplate: "{parentController}/{parentId}/{controller}/{id}",
   defaults: new { id = RouteParameter.Optional }
);

我认为混淆是由于此路由在调试模式下正常工作(就好像VS2012开发服务器从URL中删除了第一个/api)。