How to get around the exception Several actions were found in the ASP.NET Web API

When trying to find a solution for this below question:

MVC Web Api Route, default action does not work

Basically, I am faced with the "multiple actions found" problem. If the routing engine detects more than one action that is consistent with the route, it throws an exception of 500.

For example, in ValuesController there are two actions:

[HttpGet] public IEnumerable<String> Active() { var result = new List<string> { "active1", "active2" }; return result; } public IEnumerable<string> Get() { return new[] { "value1", "value2" }; } 

which match the default route:

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

WITH

 GET: /api/values 

an error will be detected. Several actions were found.

My question is: how can I get around the "multiple actions were found" exception by selecting the specified action (choosing the first consistent action is also good).

Update 1: Update from the tugberk comment, thanks.

Update 2: Update from Mike's comment, it seems the question is not very correct, so I am changing the way I ask without mentioning the routing restriction.

thanks

+4
source share
1 answer

First of all, it should not be possible for this route to correspond to the active action if you did not apply the HttpGet attribute to it. I guess you already did it. Secondly, what you want is not logical. You want to make a request for receipt and perform two identical actions. Now you use the so-called routing based on the HTTP method. For you, Iโ€™m sure that the so-called action-based routing is the best.

Assuming you have the following two actions:

 [HttpGet] public IEnumerable<String> Active() { var result = new List<string> { "active1", "active2" }; return result; } [HttpGet] public IEnumerable<string> Retrieve() { return new[] { "value1", "value2" }; } 

Your route should be as follows:

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

Then you can request those who have the following URIs:

GET / controllername / retrieve

Get / controllername / active

+6
source

All Articles