ASP.NET WebAPI将urlencoded正文中的空字符串作为null传递

Gom*_*ita 7 .net asp.net web-services urlencode asp.net-web-api

我有一个简单的ApiController

public HttpResponseMessage Put(int orderid, [FromBody] Order order)
{
    // Do something useful with order.Notes here
}
Run Code Online (Sandbox Code Playgroud)

和一个类(实际的类包含更多属性)

public class Order
{
    public string Notes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

并希望处理以下类型的PUT请求

PUT http://localhost/api/orders/{orderid}
Content-Type: application/x-www-form-urlencoded

notes=sometext
Run Code Online (Sandbox Code Playgroud)

一切正常,但空值传递为null

notes=blah            // passes blah
notes=                // Passes null
someothervalue=blah   // Passes null
Run Code Online (Sandbox Code Playgroud)

是否可以让ApiController区分空值和缺失值?

Rag*_*nti 3

您是否尝试过使用 DisplayFormatAttribute 注释该属性,例如,

public class Order
{
    [DisplayFormat(ConvertEmptyStringToNull=false)]
    public string Notes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)