Rafael Kayser Smiderle
Rafael Kayser Smiderle

Reputation: 690

MVC4 WebAPI Multiple actions were found that match the request

I have searched for a while but none of the help I've found solve my problem. I have a MVC4 WebAPI project and I get the "Multiple actions were found that match the request..." problem.

Here is my controller:

public class DataEntryController : ApiController
{
    [HttpPost]
    [ActionName("GetMessageId")]
    public HttpResponseMessage GetMessageId(HttpRequestMessage request)
    {

    }

    [HttpPost]
    [ActionName("RequestXmlDataEntry")]
    public HttpResponseMessage RequestXmlDataEntry(HttpRequestMessage request)
    {

    }

    [HttpPost]
    [ActionName("SendConfirmationXmlDataEntry")]
    public HttpResponseMessage SendConfirmationXmlDataEntry(HttpRequestMessage request)
    {

    }

    [HttpPost]
    [ActionName("SendEvent")]
    public HttpResponseMessage SendEvent(HttpRequestMessage request)
    {

    }
}

And here is my routes:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "API Default 2",
            routeTemplate: "api/{controller}/{action}");

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

Here is how my request look like:

public static string PostRequestToRestMethod(string url, string data, IWebProxy proxy, int timeout)
    {
        byte[] byteArray = Encoding.UTF8.GetBytes(data);

        string responseFromServer = string.Empty;

        HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
        webrequest.Method = "POST";
        webrequest.ContentType = "text/xml";
        webrequest.ContentLength = byteArray.Length;
        webrequest.Timeout = timeout;

        if (proxy != null)
            webrequest.Proxy = proxy;

        var dataStream = webrequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();

        using (StreamReader responseStream = new StreamReader(webresponse.GetResponseStream(), Encoding.UTF8))
        {
            responseFromServer = responseStream.ReadToEnd();
        }

        webresponse.Close();
        return responseFromServer;
    }

I'm working with .Net Framework 4.0 so WebAPI 2 is out of the question for me. Any toughts?

Upvotes: 3

Views: 2451

Answers (1)

Aftab Ahmed
Aftab Ahmed

Reputation: 1737

You don't need to create different routs. Your all three routes can be handled with this single route.

public static class WebApiConfig
{
     public static void Register(HttpConfiguration config)
     {             
         config.Routes.MapHttpRoute(
            name: "API Default",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional });
    }
}

Upvotes: 2

Related Questions