WebAPI: Multiple actions were found that match the request

Standard

If you have two or more web methods and using the “HttpGet” attribute

[HttpGet]
public HttpResponseMessage GetRelatedSearch(HttpRequestMessage request)
{
     return request.CreateResponse(HttpStatusCode.OK); 
}
 
[HttpGet]
public HttpResponseMessage GetWebSearch(HttpRequestMessage request) 
{
     return request.CreateResponse(HttpStatusCode.OK);
}

You will receive error:

2015-11-30_18-44-11

Solution:

Your by-default web api config setting would be like below in WebApiConfig.cs file under App_Start folder:

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

But in order to have multiple actions with the same http method you need to provide webapi with more information in terms of action name via the route like so:

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

Notice that the routeTemplate now includes an action.

http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

Advertisements

One thought on “WebAPI: Multiple actions were found that match the request

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s