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 } );
Action Results in Web API 2HttpResponseMessage. IHttpActionResult. Void.
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.
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.
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 } );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With