向ASP.NET Web API Controller添加显式操作路由

我有一个带有ApiController的ASP.NET Web API项目,它为User端点提供以下操作:

 GET /api/User POST /api/User DELETE /api/user 

我想提供以下端点:

 GET /api/user/metrics 

但是,当我像这样定义控制器动作时:

 [HttpGet] public HttpResponseMessage Metrics() { return null; } 

Multiple actions were found that match the request错误消息的Multiple actions were found that match the request

我理解这违反了“纯”REST API的定义,但这就是我想要的方式。 我想我必须通过映射HTTP路由来解决这个问题,但是我尝试了一些路由,但是我无法让它工作。 我的路线应该是什么样的?

默认路由不包括操作。

 routes.MapHttpRoute( name: "API Default", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); 

它根据路由器中的控制器,HTTP谓词和参数或缺少参数来选择操作。 因此,它找到了正确的控制器并寻找没有参数的GET动作。 它找到两个。

您应该添加包含该操作的其他路由。 这可以通过Kiran提到的基于属性的路由或基于约定的路由来完成。 对于基于约定的路由,路由通常放在WebApiConfig.csApplication_start()方法中。 更具体的路线在一般路线之前,所以你的路线看起来像这样:

 config.Routes.MapHttpRoute( name: "ApiWithAction", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); 

如果您使用的是Web API 2,我建议使用属性路由,这样可以使这些场景更容易体验。

对于大多数场景,您可以继续使用默认的常规路由,但可以在需要的位置使用属性路由,就像在当前场景中一样。

以下示例演示了类似的情况:

http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/WebApiAttributeRoutingSample/WebApiAttributeRoutingSample/Controllers/Api/CustomersController.cs