在ASP.NET web api中使用属性上的必需属性时出现协议webexception错误 [英] Protocol webexception error comes when using required attribute on property in ASP.NET web api

查看:52
本文介绍了在ASP.NET web api中使用属性上的必需属性时出现协议webexception错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

< br $> b $ b






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; }
}







开始提供协议错误



我尝试了什么:



试图改变JSON字符串的格式

喜欢:

string JSONString ={\value \:999.99};



但是没有工作...




start giving protocol Error

What I have tried:

Tried to change the format of JSON string
like:
string JSONString = "{\"value\":999.99}";

but did not work it...

推荐答案

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


这篇关于在ASP.NET web api中使用属性上的必需属性时出现协议webexception错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆