Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple actions were found that match the request Web API?

I am using web API and i am new in this. I am stuck in a routing problem. I have a controller with following actions :

    // GET api/Ceremony     public IEnumerable<Ceremony> GetCeremonies()     {         return db.Ceremonies.AsEnumerable();     }      // GET api/Ceremony/5     public Ceremony GetCeremony(int id)     {         Ceremony ceremony = db.Ceremonies.Find(id);         return ceremony;     }      public IEnumerable<Ceremony> GetFilteredCeremonies(Search filter)     {         return filter.Ceremonies();     } 

The problem occure when i added the action GetFilteredCeremonies to my controller. After adding this when i make an ajax call to GetCeremonies action then it return an Exception with following message :

"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were   found that match the request 

FYI: The parameter Search is the Model class which contains properties and a function name Ceremonies.

EDIT

Route:

config.Routes.MapHttpRoute(                 name: "DefaultApi",                 routeTemplate: "api/{controller}/{id}",                 defaults: new { id = RouteParameter.Optional }             ); 
like image 550
Tom Rider Avatar asked Nov 08 '12 14:11

Tom Rider


People also ask

What are the action results in Web API?

Action Results in Web API 2HttpResponseMessage. IHttpActionResult. Void.

How do you call an action method in Web API?

Default Action Methods In Web API, Get, Post, Put, and Delete verbs are used as corresponding action methods- Get(), Post ([FromBody]string value), Put (int id, [FromBody]string value), Delete (int id) for manipulating data like get, insert, update and delete.

Can we have multiple post method in Web API?

Web API provides the necessary action methods for HTTP GET, POST, PUT, and DELETE operations. You would typically pass a single object as a parameter to the PUT and POST action methods. Note that Web API does not support passing multiple POST parameters to Web API controller methods by default.


2 Answers

The problem here is your 2 Get methods will resolve to api/Ceremony and MVC does not allow parameter overloading. A quick workaround (not necessarily the preferred approach) for this sort of problem is to make your id parameter nullable e.g.

// GET api/Ceremony public IEnumerable<Ceremony> GetCeremonies(int? id) {     if (id.HasValue)     {         Ceremony ceremony = db.Ceremonies.Find(id);         return ceremony;     }     else     {         return db.Ceremonies.AsEnumerable();     } } 

However, you would then be returning a list of ceremonies when with 1 item when your trying to query for a single ceremony - if you could live with that then it may be the solution for you.

The recommended solution is to map your paths appropriately to the correct actions e.g.

context.Routes.MapHttpRoute(     name: "GetAllCeremonies",     routeTemplate: "api/{controller}",     defaults: new { action = "GetCeremonies" } );  context.Routes.MapHttpRoute(     name: "GetSingleCeremony",     routeTemplate: "api/{controller}/{id}",     defaults: new { action = "GetCeremony", id = UrlParameter.Optional } ); 
like image 30
James Avatar answered Sep 23 '22 18:09

James


If you're not requirement bound to use REST services that use api/{controller}/{id} route and attempt to resolve action based on method GET/POST/DELETE/PUT, you can modify your route to classic MVC route to api/{controller}/{action}/{id} and it will solve your problems.

like image 173
Admir Tuzović Avatar answered Sep 23 '22 18:09

Admir Tuzović