Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API Routing - multiple actions were found that match the request

I got this Route:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { id = UrlParameter.Optional }
        );

And this Actions:

    [System.Web.Http.HttpPost]
    [System.Web.Http.ActionName("GetLoginSeed")]
    public object GetLoginSeed()

    [System.Web.Http.HttpPost]
    [System.Web.Http.AllowAnonymous]
    [System.Web.Http.ActionName("Authenticate")]
    public object PerformLogin(JObject jr)

This is the Post Request:

    http://localhost:61971/api/Login/GetLoginSeed

Why I always get an multiple actions were found that match the request error?

like image 569
TheJoeIaut Avatar asked Jun 17 '13 10:06

TheJoeIaut


People also ask

What are the two ways to implement routing in Web API?

It routes an incoming HTTP request to a particular action method on a Web API controller. Web API supports two types of routing: Convention-based Routing. Attribute Routing.

Can we have multiple get methods in Web API?

As mentioned, Web API controller can include multiple Get methods with different parameters and types. Let's add following action methods in StudentController to demonstrate how Web API handles multiple HTTP GET requests.

What is Web API routing?

Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.


1 Answers

I got this Route:

What you have shown is a route for MVC controllers. I hope you realize that Web API controllers are an entirely different thing. They have their own routes defined in the ~/App_Start/WebApiConfig.cs.

So make sure tat you have included the {action} token in your Web API route definition (which I repeat once again has nothing to do with your MVC route definitions):

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}"
);
like image 163
Darin Dimitrov Avatar answered Oct 06 '22 13:10

Darin Dimitrov