Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Web Api not binding model on POST

I'm trying to POST JSON data to a Web Api method but the JSON data is not binding to the model.

Here's my model:

[DataContract]
public class RegisterDataModel
{
    [DataMember(IsRequired = true)]
    public String SiteKey { get; set; }

    [DataMember(IsRequired = true)]
    public String UserId { get; set; }

    [DataMember(IsRequired = true)]
    public String UserName { get; set; }
}

Here's my Web Api action:

    public class RegisterController : ApiController
    {
    public Guid Post([ModelBinder] RegisterDataModel registerDataModel)
    {
        if (!ModelState.IsValid)
        {
            throw new ModelStateApiException(ModelState);
        }
        var userProfileDataContract = userProfileBusinessLibrary.GetNewOne();
        userProfileDataContract.UserId = registerDataModel.UserId;
        userProfileDataContract.UserName = registerDataModel.UserName;

        var userKey = userProfileBusinessLibrary.Register(registerDataModel.SiteKey, userProfileDataContract);

        return userKey;
    }
    }

Before I added [ModelBinder], registerDataModel was null. After adding [ModelBinder], registerDataModel is a RegisterDataModel instance, but all of the property values are null.

Here's my Request via Fiddler:

http://local.testwebsite.com/api/register

Request Headers:
User-Agent: Fiddler
Host: local.testwebsite.com
Content-Length: 89
Content-Type: application/json; charset=utf-8:

Request Body:
{ 
 "SiteKey":"qwerty",
 "UserId": "12345qwerty", 
 "UserName":"john q"
}    

What am I missing to make my post data bind to the RegisterDataModel properties? Thanks for your help.

like image 805
Tom Schreck Avatar asked Oct 06 '12 16:10

Tom Schreck


People also ask

What is difference between FromQuery and FromBody?

[FromQuery] - Gets values from the query string. [FromRoute] - Gets values from route data. [FromForm] - Gets values from posted form fields. [FromBody] - Gets values from the request body.

What is FromBody and FromUri in Web API?

The [FromUri] attribute is prefixed to the parameter to specify that the value should be read from the URI of the request, and the [FromBody] attribute is used to specify that the value should be read from the body of the request.

How do I bind data in Web API?

Easiest way is to add ModelBinder attribute to the parameter. In following example, I have added ModelBinder attribute to the "data" parameter, so web API can understand how to use custom model binder, while binding the data. Another way is to add ModelBinder attribute to the type.


2 Answers

Not related to the OP's problem, but the title of the question led me here when I used (public) fields instead of properties in the Model class (i.e. no {get; set;}). It turned out that this also causes the binding to fail.

Maybe helps someone.

like image 152
robert4 Avatar answered Oct 07 '22 09:10

robert4


How are you creating the JSON request? Through Fiddler request builder? Try just the following in the request body.

{ 
 "SiteKey":"qwerty",
 "UserId": "12345qwerty", 
 "UserName":"john q"
}

I'm guessing 'Request Body:' is also part of your request body. Remove that and check.

like image 25
Badri Avatar answered Oct 07 '22 11:10

Badri