3

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?

4
  • and this is youre only code? Commented Jun 16, 2016 at 14:29
  • No, I removed the inner portion of each method. Is there any other portion that could help me be more specific? Commented Jun 16, 2016 at 14:31
  • 1
    do you also have a WebApiConfig next to youre RouteConfig .. and how does youre request looks like Commented Jun 16, 2016 at 14:36
  • Oh I see, I've corrected the question. I moved the routes to the correct class but still the same error happened. Commented Jun 16, 2016 at 14:43

1 Answer 1

2

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 }); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.