2
votes

I am trying to call WEBAPI from postman .My AI method is post but when i execute it i get below given error

Multiple actions were found that match the request:***

Below is my Code: webapi route.config

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

        // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
        // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
        // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
        config.EnableQuerySupport();

        // To disable tracing in your application, please comment out or remove the following line of code
        // For more information, refer to: http://www.asp.net/web-api
        config.EnableSystemDiagnosticsTracing();
    }
}

APIController:

 public class MembershipController : ApiController
    {
      [System.Web.Http.ActionName("Upload")]
        public HttpResponseMessage Upload(string employeedetails)
        {
             `Some Code`
         }

      [System.Web.Http.ActionName("BulkUpload")]
 [System.Web.Http.HttpPost]
        public HttpResponseMessage BulkUpload(string employeedetails)
        {
                `Some Code`
         }
            [System.Web.Http.ActionName("Transfer")]
            public HttpResponseMessage Transfer(string employeedetails)
              {
                      `Some Code`
               }

         }

I am not getting whats going wrong method has different action name and route config is also fully qualified api url which contain controller method and id as optional parameter.To identify url this should be sufficient but it's not working. Am i missing anything?

2
there is also a ton of stuff out there on this already> stackoverflow.com/questions/14534167/…Rob Allen
So {method} should be {action} @RobA?mjwills
@Pavan Does this work or do you still have an issue?Win
@win this issue is not yet solved.But What i Observed is when i pass Data in Query String it works but unfortunately query string has limits on size and my data is more than that.When i use httpcontext.Current.Request.Form.GetValue(0); I get the value in string format but i want it in method parameter rather than writing a code in method to get it. How to achieve this?Pavan

2 Answers

2
votes

If this is RESTful API, you cannot have three HttpPost, unless you differentiate them by the URL slugs.

Easiest way is to use attribute route. E.g.

public class MembershipController : ApiController
{
    // POST api/Membership/data/Upload
    [Route("api/Membership/{employeedetails}/Upload")]
    [HttpPost]
    public HttpResponseMessage Upload(string employeedetails)
    {
        `Some Code`
    }

    // POST api/Membership/data/Bulk
    [Route("api/Membership/{employeedetails}/Bulk")]
    [HttpPost]
    public HttpResponseMessage BulkUpload(string employeedetails)
    {
        `Some Code`
    }

    // POST api/Membership/data/Transfer
    [Route("api/Membership/{employeedetails}/Transfer")]
    [HttpPost]
    public HttpResponseMessage Transfer(string employeedetails)
    {
        `Some Code`
    }
}
0
votes

Solution 1:

I have added Route Config in WebApiConfig class

 public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {

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

    // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
    // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
    // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
    config.EnableQuerySupport();

    // To disable tracing in your application, please comment out or remove the following line of code
    // For more information, refer to: http://www.asp.net/web-api
    config.EnableSystemDiagnosticsTracing();
}

}

Solution 2

public class MembershipController : ApiController
{
  [System.Web.Http.ActionName("Upload")]
    public HttpResponseMessage Upload([FromBody]string employeedetails)
    {
         `Some Code`
     }
[System.Web.Http.HttpRoute("api/membeship/BulkUpload")]
 [System.Web.Http.HttpPost]
        public HttpResponseMessage BulkUpload(string employeedetails)
        {
                `Some Code`
         }
[System.Web.Http.HttpRoute("api/membeship/Transfer")]
            public HttpResponseMessage Transfer([FromBody]string employeedetails)
              {
                      `Some Code`
               }

         }

If we compare Solution 1 and solution 2 then Solution 1 will work but it need a query string parameter where as sollution 2 will also work for post parameters(FormBody)

I am looking in details what does solution 2 makes a difference. Because when we remove HTTPRoute From solution 2 then it's also need query string parameter only and if we try to pass parameter using post then it get passed as null value. Very soon i will find it out and will share detail analysis on stack overflow.

Happy Coding