[英]Attribute routing to two similar actions on the same controller
I have a controller with the two following actions: 我有一个执行以下两个操作的控制器:
[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)
{ .... }
When i make a get request to the following url: 当我对以下网址发出get请求时:
...api/organization/2473ce5e-42e6-449f-9528-a29000921ded/person/1/getpersons
I get this error: 我收到此错误:
Multiple actions were found that match the request.
Both GetManagers and GetPersons match. GetManagers和GetPersons都匹配。 Why is this?
为什么是这样? Why does the "/getpersons" at the end of my url not matter?
为什么我网址末尾的“ / getpersons”没关系? What can i do to make them separately identifiable?
我该怎么做才能分别识别它们?
In Web API the request are mapped to the actions based on HTTP verbs.Change the Default route in WebApiConfig.cs and ensure that you have 在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 }
);
hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.