Akmal Salikhov
Akmal Salikhov

Reputation: 872

WEB API. Multiple actions were found that match the request

Need help.

I have 2 controllers:

// POST (Single SMS)
[ActionName("AddSMS")]
public HttpResponseMessage Post(MySMS singleSMS)
{
    try
    {
        SMS_Repository.Add(singleSMS);
        return Request.CreateResponse<MySMS>(HttpStatusCode.Created, singleSMS);
    }
    catch (Exception)
    {
        return Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Error");
    }
}

// POST (Collection of SMSes)
[ActionName("AddSMSCollection")]
public HttpResponseMessage Post(List<MySMS> smses)
{
    try
    {
        SMS_Repository.Add(smses);
        return Request.CreateResponse<List<MySMS>>(HttpStatusCode.Created, smses);
    }
    catch (Exception)
    {
        return Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "errorus");
    }
}

and Route:

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional, action = RouteParameter.Optional }

Now. If I send request like this:

localhost:25856/api/sms/AddSMSCollection

it works

Is it possible to tune route so that I can use localhost:25856/api/sms and didn't get Multiple actions were found that match the request error??

sorry for my bad english..

Upvotes: 0

Views: 458

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039398

You could define the action that should be executed in this case:

defaults: new { id = RouteParameter.Optional, action = "AddSMS" }

But with only the following url localhost:25856/api/sms and not including the action name, I hope you realize that the routing engine has no way of disambiguate which action to execute. The routing engine could use the HTTP verb but in your case both actions are POST.

Upvotes: 1

Related Questions