属性路由到同一控制器上的两个类似操作

时间:2014-10-30 09:52:40

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

我有一个控制器,其中包含以下两个操作:

[Route("api/organization/{orgId}/person/{ver}/getmanagers", Name = "GetManagers")]
public HttpResponseMessage GetManagers(Guid orgId, int ver)
{ .... }

[Route("api/organization/{orgId}/person/{ver}/getpersons", Name = "GetPersons")]
public HttpResponseMessage GetPersons(Guid orgId, int ver)
{ .... }

当我向以下网址发出请求时:

...api/organization/2473ce5e-42e6-449f-9528-a29000921ded/person/1/getpersons

我收到此错误:

Multiple actions were found that match the request.

GetManagers和GetPersons都匹配。为什么是这样?为什么我的网址末尾的“/ getpersons”无关紧要?我该怎么做才能使它们单独识别?

1 个答案:

答案 0 :(得分:0)

在Web API中,请求被映射到基于HTTP谓词的操作。更改WebApiConfig.cs中的默认路由并确保您拥有

//Required for Attribute based routing
config.MapHttpAttributeRoutes();

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

希望这会有所帮助。

相关问题