0

My Html code is something like this.

<input type="text" name="Id" value="123"/>
<input type="text" name="Name" value="XYZ"/>
<input type="text" name="Abc.Address" value="ABC,XYZ 700567"/>
<input type="text" name="Abc.Mobile" value="6958743216"/>

Classes:-

class A
{
    public string Id { get; set; }
    public string Name { get; set; }
    public Abc Abc { get; set; }
}

class Abc
{
    public string Address { get; set; }
    public string Mobile{ get; set; }
}

Through upper input When I am calling MVC controller the then binding happen. means for following method

public ActionResult Post(A x)

value in x is

x.Id="123"
x.Name="XYZ"
x.Abc.Address="ABC,XYZ 700567"
x.Abc.Mobile="6958743216"

but when I tried same things in web api it is giving me 415(Unsupported media type). means for following method

public IHttpActionResult Post(A x)

instead of binding it is giving 415 status code saying Unsupported Media Type. I know it is stupid to ask but why this happen? and if I want to do same binding as happen in mvc how can I achieve that?(I don't want to do something like this --> public IHttpActionResult Post([SomeBinder]A x))

Is there way to achieve this or Not?

yajiv
  • 2,901
  • 2
  • 15
  • 25
  • How are you posting the data? –  Feb 27 '18 at 11:41
  • _How are you posting the data? _ don't get it. If you mean how I send this data then ans is through form-submit – yajiv Feb 27 '18 at 11:43
  • 3
    Web API should not be used directly against HTML forms, that's what MVC is for – Camilo Terevinto Feb 27 '18 at 11:43
  • So if I want to make api, then I how will I do it? Is there any work around? – yajiv Feb 27 '18 at 11:44
  • are you calling this action method from fiddler or postman? – Rakesh Burbure Feb 27 '18 at 11:47
  • Possible duplicate of [Error 415 Unsupported Media Type: POST not reaching REST if JSON, but it does if XML](https://stackoverflow.com/questions/11773846/error-415-unsupported-media-type-post-not-reaching-rest-if-json-but-it-does-if) – Rakesh Burbure Feb 27 '18 at 11:58
  • Possible duplicate of [How to get POST data in WebAPI?](https://stackoverflow.com/questions/17832784/how-to-get-post-data-in-webapi) – Crowcoder Feb 27 '18 at 12:00
  • @Crowcoder the answer given in link not solving problem at all(still giving same error). – yajiv Feb 27 '18 at 12:09
  • show how you make the request in PostMan then, if that's what's giving you the 415 error. – ADyson Feb 27 '18 at 12:23
  • from postman in body I select form-data and then pass name as key from input tag above and value as value and then send in response it is giving 415. – yajiv Feb 27 '18 at 12:27
  • Show exactly (screenshot maybe) if you can. A description of something doesn't mean it was done correctly. Also, test if the API method works if you use JSON instead. – ADyson Feb 27 '18 at 12:45
  • It is working for json – yajiv Feb 27 '18 at 12:58

1 Answers1

0

It has to do with the Content-Type of the request.

Your WebAPI endpoint is expecting your post request to have the Content-Type application/json. Once you add that it will be accepted.

However if you want to post an HTML form to a controller you shouldn't use WebAPI. If you have to then you can serialize the form and submit the form using javascript

Nick Chapsas
  • 6,872
  • 1
  • 20
  • 29