2

I'm trying to post a JSON object to a Web Api URL and it's not binding to the model.

This seems to be the same problem: ASP.Net Web Api not binding model on POST

I tried everything that they did and it still doesn't work. The one difference you may notice is that I'm not using the DataContract attributes, but I don't believe they should be required, and didn't make any difference when I tried them.

public class MyModel
{
    public int Id { get; set; }
}

Public class MyController : ApiController
{
    public int Save(MyModel myModel)
    {
        // myModel is always null
        return 0;
    }
 }

Fiddler setup

Community
  • 1
  • 1
Josh Russo
  • 3,080
  • 2
  • 41
  • 62
  • How is your web.api routing look like? Try to specify the accept header in fiddler. Add this line in Request headers: `Accept: "application/json"` – nemesv Jan 05 '13 at 23:01
  • Nope no, dice. I tried with and without quotes – Josh Russo Jan 05 '13 at 23:04
  • @nemesv thats actually incorrect, `Accept:` is used for the acceptable return types when you do a get, not on post where you use content-type to specify the type of the body – undefined Jan 05 '13 at 23:05

2 Answers2

5

You appear to be missing [HttpPost] attribute from your controller method. It appears in the above case this is actually not strictly required, perhaps this is only needed when posting primitives?

Also just as a note I would use a more REST based syntax if you are using WebApi for example use methods Get, Post, Put ect on your controller rather than named methods

EDIT:

You also have one other really subtle issue with your post. A header line cant end with a ; so Content-Type: application/json; charset=utf-8; should be Content-Type: application/json; charset=utf-8

undefined
  • 33,537
  • 22
  • 129
  • 198
  • When I tried to request it via Get it said that it wasn't supported. I'm assuming that it's defaulting to Post because it's accepting an object that may be large. All of the other interfaces defaulted to Get – Josh Russo Jan 05 '13 at 23:03
  • 1
    @JoshRusso I'm pretty sure that you need to attribute post methods unless they are named Post – undefined Jan 05 '13 at 23:06
  • I tried naming it Post after your comment and it still behaves the same – Josh Russo Jan 05 '13 at 23:06
  • @JoshRusso ill give it a go and get back to you when my VS upgrades – undefined Jan 05 '13 at 23:14
  • I just created a fresh sample web api project and that doesn't even work. I wonder if there is a problem with my install – Josh Russo Jan 06 '13 at 00:59
  • @JoshRusso i gave your sample a bit of a run and made it go, see my edit – undefined Jan 06 '13 at 01:50
  • Yup, it was the `;` at the end! Sheesh, I spent hours on that one. Thanks! – Josh Russo Jan 06 '13 at 02:04
  • @JoshRusso No problem, was a bit of a hard one to spot :) – undefined Jan 06 '13 at 02:15
  • @JoshRusso you can use the Dev HTTP Client to avoid these kind of errors which is less error prone for constructing raw requests than fiddler. https://chrome.google.com/webstore/detail/dev-http-client/aejoelaoggembcahagimdiliamlcdmfm?hl=en – wal Jan 06 '13 at 02:19
  • Thanks for the link though the dev http client doesn't seem to be able to be picked up by Fiddler. This one has a proxy setting that makes it compatible with Fiddler so you can really see the request and response in detail http://code.google.com/p/rest-client/ – Josh Russo Jan 06 '13 at 03:15
0

It could be an encoding issue. I changed my encoding and model binding is done succesfully.

client.Encoding = Encoding.UTF8;
Ahmet Arslan
  • 5,380
  • 2
  • 33
  • 35