Brian
Brian

Reputation: 1387

ASP .NET Web API Post modelbinding issues

I am a web API newbie, trying my first POST to the API (all my GETs are fine). And no matter what I try, model binding doesn't seem to work. Routing is fine, because I see control coming to the correct method, but model object references is always null. I tried with a simple class as model (two properties, both strings), still no luck. Any suggestions on what I am missing? I am using fiddler to post. eg: request body has

{"Name":"Test","Description":"Test"}

Tried with a = at the start, Tried method-argument-variable= at the start, no luck. null always.

Upvotes: 1

Views: 274

Answers (1)

Ralaad
Ralaad

Reputation: 79

When you sending the parameter by request body , you must use the [FromBody] attribute in method body. [FromBody] denotes it binds the parameters from request to the model class. Check whether you are using [FromBody] or by url ?

[System.Web.Http.HttpPost]
public List<PortalUser> GetPortalUsers([FromBody]PortalUser PortalUser)
{ }

Upvotes: 1

Related Questions