ASP.NET WebAPI在urlencoded主体中将空字符串传递为null [英] ASP.NET WebAPI passing empty string in urlencoded body as null
本文介绍了ASP.NET WebAPI在urlencoded主体中将空字符串传递为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的ApiController
I have a simple ApiController
public HttpResponseMessage Put(int orderid, [FromBody] Order order)
{
// Do something useful with order.Notes here
}
和一个类(实际的类包含更多属性)
and a class (the actual class contains several more properties)
public class Order
{
public string Notes { get; set; }
}
并希望处理以下类型的PUT请求
and wish to handle PUT requests of the following type
PUT http://localhost/api/orders/{orderid}
Content-Type: application/x-www-form-urlencoded
notes=sometext
一切正常,但是将空值传递为null
Everything works fine, but empty values are passed as null
notes=blah // passes blah
notes= // Passes null
someothervalue=blah // Passes null
是否可以让ApiController区分空值和缺失值?
Is it possible to have ApiController distinguish between empty values and missing values?
推荐答案
您是否尝试过使用DisplayFormatAttribute注释属性,例如
Have you tried annotating the property with DisplayFormatAttribute, like,
public class Order
{
[DisplayFormat(ConvertEmptyStringToNull=false)]
public string Notes { get; set; }
}
这篇关于ASP.NET WebAPI在urlencoded主体中将空字符串传递为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文