Home > Mobile >  BindProperty is not binding to incoming JSON in request body
BindProperty is not binding to incoming JSON in request body

Time:12-15

This the request's body:

{
    userId: 382,
    serviceName: 'Translation'
}

And this is a property on my API controller:

[BindProperty]
public string ServiceName { get; set; }

But it's null. How should I configure ASP.NET Core to bind from JSON too?

I'm using .NET 6.

CodePudding user response:

Perhaps make FromBody explicit and specify the name, in case it is related to the casing not matching.

 [FromBody(Name = "serviceName")]

CodePudding user response:

//Creating a class for the method

public class data{
     public int UserId {get; set;}
     public string Service{get; set;}
}

//If you wish to do in method
public void getdata([FromBody] data recivedata){

data d = new data();
d.UserId = recivedata.UserId;
d.Service = recivedata.Service;

//perform any action with the data
}

  • Related