Multiple Actions Were Found That Match the Request in Web API

Multiple actions were found that match the request in Web Api

Your route map is probably something like this in WebApiConfig.cs:

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 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. Lots more info here: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

Update:

Alright, now that I think I understand what you are after here is another take at this:

Perhaps you don't need the action url parameter and should describe the contents that you are after in another way. Since you are saying that the methods are returning data from the same entity then just let the parameters do the describing for you.

For example your two methods could be turned into:

public HttpResponseMessage Get()
{
return null;
}

public HttpResponseMessage Get(MyVm vm)
{
return null;
}

What kind of data are you passing in the MyVm object? If you are able to just pass variables through the URI, I would suggest going that route. Otherwise, you'll need to send the object in the body of the request and that isn't very HTTP of you when doing a GET (it works though, just use [FromBody] infront of MyVm).

Hopefully this illustrates that you can have multiple GET methods in a single controller without using the action name or even the [HttpGet] attribute.

Multiple actions were found that match the request C#

Update WebApiConfig route template to recognize action names in the route template as the default convention based route template is usually api/{controller}/{id}

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Attribute routing.
config.MapHttpAttributeRoutes();

// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}

You should also update the actions with [HttpPost] attributes.

public class DocumentController : ApiController {

[HttpPost]
public async Task<IHttpActionResult> CreateDocument([FromBody]List<DocumentDetails> details) {
//...
}

[HttpPost]
public async Task<IHttpActionResult> Create([FromBody]AppDocument document) {
//...
}
}

WebApi 'Multiple actions were found that match the request' error after making call in angular

The routing on web api works on this way

  • To find the controller, Web API adds "Controller" to the value of the {controller} variable.

  • To find the action, Web API looks at the HTTP method, and then looks for an action whose name begins with that HTTP method name. For example, with a GET request, Web API looks for an action that starts with "Get...", such as "GetContact" or "GetAllContacts". This convention applies only to GET, POST, PUT, and DELETE methods. You can enable other HTTP methods by using attributes on your controller. We’ll see an example of that later.
    Other placeholder variables in the route template, such as {id}, are mapped to action parameters.

  • Instead of using the naming convention for HTTP methods, you can explicitly specify the HTTP method for an action by decorating the action method with the HttpGet, HttpPut, HttpPost, or HttpDelete attribute.

That's why is matching 2 actions, the name of the method is not used "by default" to match an action. You may have something like this to make it work

public class WebApiInitializer : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
RouteTable.Routes.MapHttpRoute(
"CreateJob",
"api/msi/SBJob/CreateJob",
defaults: new {Controller = "SBKob", Action = "CreateJob"}
);

RouteTable.Routes.MapHttpRoute(
"GetJob",
"api/msi/SBJob/GetJob",
defaults: new {Controller = "SBKob", Action = "GetJob"}
);
}
}

Multiple actions were found that match the request on get request

Controller doesn't know what the name of your route value - is it item_code or id. I recommend you to use this route attributes:

Route("~/api/SPSOnHandByItemCode/{item_code}")]
public IEnumerable<SPS_ONHAND> GetByItemCode(string item_code)

and

[Route("~/api/SPSOnHandById/{id}")]
public HttpResponseMessage GetById(int id)

WEB API Routing Error Multiple actions were found that match the request with different API paths

The reason for the error in OP is that the routing table could not differentiate between the two action based on the route parameters in the template and that both action have the same HTTP Method (POST)

Narrow the mapping (route) by using defaults parameter when mapping.

config.Routes.MapHttpRoute(
name: "getMultiProbe",
routeTemplate: "api/v1/{controller}/probe/{server}",
defaults: { controller = "Hist", action = "getMultiProbe" }
);

config.Routes.MapHttpRoute(
name: "getCurrentMultiProbe",
routeTemplate: "api/v1/{controller}/currentmultiprobe/{server}",
defaults: { controller = "Hist", action = "getCurrentMultiProbe" }
);

Multiple actions were found that match the request: WebAPI 2

You could use attribute routing for this.

Define the route as a string in the Route attribute ontop of the methods as this

[Route("api/controller/Post1")]
[HttpPost]
public GetDocumentsResponse Post([FromBody]GetDocumentsRequest request)
{

}
[Route("api/controller/Post2")]
[HttpPost]
public FinishDocumentsResponse Post([FromBody] FinishDocumentsRequest request)
{

}

Multiple actions were found that match the request in mvc5 api application

It happens because your second action name starts with Get... and also it has the same parameter like first action (IsStoreKeyValid(string storeName, string storeKey))

By default, if action is HttpGet then it searches action name which starts with GetSomething
and check parameter. More Info MSDN

You need to change the second function name or need to use attribute routing

ex.[Route("pageIds")]

more info attribute routing

Web Api. Multiple actions were found that match the request. How to add custom endpoint

I would recommend using Attribute routing in these cases, where you have Multiple Action methods for same http verb .

Below will work,

Add this line above all defined routes in Register Method

config.MapHttpAttributeRoutes(); // Enable Attribute Routing Web API routes

and define routes as attributes..

[RoutePrefix("internal/api/therapist")]
public class TherapistController : ApiController
{
[HttpPost]
[Route("Post/{therapistModel}")]
public IHttpActionResult Post(TherapistModel therapistModel)
{
return Ok();
}

[HttpPost]
[Route("Email/{therapistModel}")]
public IHttpActionResult SendConfirmationEmail(TherapistModel therapistModel)
{
return Ok();
}
}

In you JS calling code..

internal/api/therapist/post/{yourparameter}

and

internal/api/therapist/email/{yourparameter}
....to call above methods accordingly



Related Topics



Leave a reply



Submit