简体   繁体   English

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

[英]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.

相关问题 Web API 2 / MVC 5:属性路由将参数作为查询字符串传递,以在同一控制器上定位不同的操作 - Web API 2 / MVC 5 : Attribute Routing passing parameters as querystring to target different actions on same controller 是否可以在同一个控制器中使用属性路由和基于约定的路由? - Is it possible to use attribute routing and convention based routing in the same controller? 路由和控制器操作-可选数据 - Routing and Controller Actions - Optional Data 对同一控制器操作使用属性路由和全局路由 - Using attribute routing and a global route for the same controller action 如果我在 mvc5 中将默认路由和属性路由设置为相同的 controller 则常规路由不起作用 - If I set default route and attribute routing to same controller in mvc5 then conventional routing is not working Web API路由两个控制器类似的操作,并且参数未在一个操作中填充参数值 - Web api routing two controller similar action and parameter not populating parameter value in one action WebAPI 控制器继承和属性路由 - WebAPI controller inheritance and attribute routing 具有两个GET操作的WebApi控制器 - WebApi Controller with two GET actions 将 CustomId 和 Id 路由到相同的 controller - Routing CustomId and Id to same controller 使用属性路由从URL中删除控制器名称 - Remove controller name from URL with Attribute routing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM