Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将x-www-form-URLX的Web API MediaTypeFormatter编码为DefaultValue属性_C#_Asp.net Web Api_Mediatypeformatter - Fatal编程技术网

C# 将x-www-form-URLX的Web API MediaTypeFormatter编码为DefaultValue属性

C# 将x-www-form-URLX的Web API MediaTypeFormatter编码为DefaultValue属性,c#,asp.net-web-api,mediatypeformatter,C#,Asp.net Web Api,Mediatypeformatter,假设我有一个模型,看起来像: public class ToggleableResource { [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] [DefaultValue(true)] public bool Enabled { get; set; } public string Value { get; set; } } public class FormatterD

假设我有一个模型,看起来像:

public class ToggleableResource
{
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
    [DefaultValue(true)]
    public bool Enabled { get; set; }

    public string Value { get; set; }
}
public class FormatterDefaultsController : ApiController
{
    public ToggleableResource Post([FromBody] ToggleableResource resource)
    {
        return resource;
    }
}
{
  "Enabled": true,
  "Value": "some resource state"
}
我有一个控制器,看起来像:

public class ToggleableResource
{
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
    [DefaultValue(true)]
    public bool Enabled { get; set; }

    public string Value { get; set; }
}
public class FormatterDefaultsController : ApiController
{
    public ToggleableResource Post([FromBody] ToggleableResource resource)
    {
        return resource;
    }
}
{
  "Enabled": true,
  "Value": "some resource state"
}
如果我向
/api/formatterDefaults/
提交
POST
,内容类型为
应用程序/json
,正文为:

{ "Value": "some resource state" }
value=some+resource+state
我得到的值如下所示:

public class ToggleableResource
{
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
    [DefaultValue(true)]
    public bool Enabled { get; set; }

    public string Value { get; set; }
}
public class FormatterDefaultsController : ApiController
{
    public ToggleableResource Post([FromBody] ToggleableResource resource)
    {
        return resource;
    }
}
{
  "Enabled": true,
  "Value": "some resource state"
}
这表明
已启用
正在从
DefaultValue
属性获取
true
。然而


如果我再次向
/api/formatterDefaults/
提交
帖子,但内容类型为:application/x-www-form-urlencoded
,正文为:

{ "Value": "some resource state" }
value=some+resource+state
我得到的值是:

{
  "Enabled": false,
  "Value": "some resource state"
}
当请求正文中省略了
DefaultValue
属性时,我有没有办法让(或任何类负责的)遵守该属性并将
Enabled
设置为
true