Web API中的Uri路径扩展映射

Mik*_*mbe 2 .net routes asp.net-mvc-4 asp.net-web-api

我正在尝试在Web API中使用Uri路径扩展映射.我正确设置了路线:

routes.MapHttpRoute(
                       name: "Api with extension",
                       routeTemplate: "api/{controller}.{ext}/{id}",
                       defaults: new { id = RouteParameter.Optional, ext = RouteParameter.Optional }
                   );

routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
Run Code Online (Sandbox Code Playgroud)

并且它可以使用默认路由,例如/api/products.json/123但是对于其他方法,例如/api/products.json?categoryid=5我收到一条错误消息:

"No action was found on the controller 'Products' that matches the request."
Run Code Online (Sandbox Code Playgroud)

所有路由工作都找不到扩展名,例如/ api/products/123/ api/products?categoryid = 5.任何想法,我缺少什么?

小智 8

如果有其他人遇到这个,那么答案(如何让Uri扩展映射工作)可能是以下(这对我自己有用,经过很多挫折和搜索)

将以下内容添加到web.config中,进入System.WebServer部分

<modules runAllManagedModulesForAllRequests="true"/>
Run Code Online (Sandbox Code Playgroud)