Home > Software engineering >  Asp.Net Web Api 5: parameter not binding
Asp.Net Web Api 5: parameter not binding

Time:12-11

I have a simple api endpoint and I want to pass a JSON object

The controller code:

    [HttpPost, Route("member/list")]        
    public IHttpActionResult MemberList([FromBody] MemberListRequest listRequest)
    {
        var members = MemberService.ListMembers(listRequest);
        return Ok(members);
    }

The parameter class:

[Serializable]
public class MemberListRequest
{
    public int? CountryId { get; set; }
    public int? RegionId { get; set; }
    public int? UnitId { get; set; }
    public int? StatusId { get; set; }
    public bool IncludeDeleted { get; set; }
    public string Text { get; set; }
}

When I call my api from Postman (like in the image) I get a listRequest object with all the members set to default values. Content-Type is set to "application/json" in the request headers.

Postman call

This is MVC 5. Any suggestion? What am doing wrong?

CodePudding user response:

Okay, I am really surprized. It turns out that the [Serializable] attribute was preventing the parameter to bind correctly.

So I just removed it, and now it works.

public class MemberListRequest
{
    public int? CountryId { get; set; }
    public int? RegionId { get; set; }
    public int? UnitId { get; set; }
    public int? StatusId { get; set; }
    public bool IncludeDeleted { get; set; }
    public string Text { get; set; }
}
  • Related