0

I m calling a WebApi method and passing data to it. While debugging I can see my control go to WebAPI method but it is not receiving correct parameter data which I m passing to it. While debugging I can see data (a string) is being passed to WebApi but on next step WebApi receives null. Is some configuration required to received parameter ? Please help me below is my code:

 public static string PostData(Uri url,string obj)
       {
          string data = null;

           try
           {                          

               using (WebClient proxy = new WebClient())
               {                   
                   proxy.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                   data = proxy.UploadString(url,"Post",obj);
               }
           }
           catch (Exception ex)
           {               
               throw ex;
           }
           return data;
       }

WebAPI

 [HttpPost]
        public void Post([FromBody]string data)
        {
            leadOptService.AddListOptions(data);            
        }

WebApi Conifg:

 public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
user576510
  • 5,777
  • 20
  • 81
  • 144

2 Answers2

1

You haven't mapped your POST to any route. So with the default route, its trying to match it with the id parameter. You can define a new route for your API like this:

[RoutePrefix("myController")]
public class MyController : ApiController
{
    [Route("")]
    public void Post([FromBody]string data)
    {
        leadOptService.AddListOptions(data);            
    }
}

Assuming your controller is called MyController, you would call it via POST api/mycontroller.

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • thanks for reply, I did but there is an error "The remote server returned an error: (405) Method Not Allowed." In debug control is not going to WebApi. Please help – user576510 Oct 02 '14 at 05:56
  • I tried call in fidler and receive this messae "{"Message":"The requested resource does not support http method 'POST'."}" In my calling method I have "Post" as well. Please guide. – user576510 Oct 02 '14 at 06:03
  • This issue in my view is not related to routing since the call is getting routed correctly, it's more of schema mismatch between json data passed and input parameter receiving it – Mrinal Kamboj Oct 02 '14 at 06:18
  • He's using a simple type, _AND_ forcing Web API to read it from body by using `[FromBody]`. In this case, there is no object in play so there is no JSON schema mismatch. – Mrchief Oct 02 '14 at 13:45
1

In my view the issue out here is different, to clarify the question, you are passing Json data to a WebAPI post method via http body and though you are able to see the call breaking in the web api but no data gets filled, the input data remains null

Reason for that is the mismatch between the schema of data passed and the parameter receiving it, you are using Json to pass a string in the following parameter:

[FromBody]string data

Json is always a key value pair, so your http body should of the format:

{
"data":"Message"
}

this would ensure that "Message" gets filled up in the input parameter

Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74
  • thanks, I have tried passing just my name but it is still null. – user576510 Oct 02 '14 at 06:33
  • To clarify from my MVC controller I can pass a string (just my name or an object serialized) to helping method giving in question. But from helping method to WebApi method receive argument as null. – user576510 Oct 02 '14 at 06:34
  • Last Json was incorrect, please look at the edited version, key also needs to be a in the quotes. Just tried at Json Lint validator – Mrinal Kamboj Oct 02 '14 at 06:37
  • Mostly I have seen schema mismatch as the reason for not filling the input values in Web API from http body. If this doesn't work then use developer tools in chrome to check the body of the message passed, whether that is correctly formatted and passed, if that is not correct, then webapi will bound to get a null – Mrinal Kamboj Oct 02 '14 at 06:45
  • can you please confirm my way to call WebApi is correct ? – user576510 Oct 02 '14 at 07:09
  • Check the link below, it has example, in C# Json string would need escape characters that would come automatically when you serialize to Json: http://stackoverflow.com/questions/15091300/posting-json-to-url-via-webclient-in-c-sharp Answer explains the process of data creation for posting to the Web API – Mrinal Kamboj Oct 02 '14 at 07:21