0
votes

I have created ASP.NET Web API and calling the post method using json from client side and it's working fine :

public class ValueController : ApiController

{
    public void Post([FromBody]model value)
    {

    }
}

public class model
{
    public decimal value { get; set; }
}

string JSONString = "{\"value\":\"999.99\"}";

but when I use the [Required] attribute in the value property,

public class model

{
    [Required]
    public decimal value { get; set; }
}

it's start giving protocol Error when call the GetResponse() method..

1

1 Answers

0
votes

Since decimal is a Value type and if we use the Required attribute to a value type, it will cause an error.. it will always have a value (the default of 0) if the incoming request does not provide the value.

Now I am using [DataMember(isRequired=true)] instead of [Required] attribute