当我在调试器中运行测试时,我的 ApiController 方法有时会收到空参数。但是,如果我触摸控制器文件并重新构建它,它可以在相同的测试中正常工作。一切都是一样的,只是它重新编译了 Web Api 项目的 DLL。我打开了跟踪,但除了参数是否为空之外,它什么也没告诉我。这怎么可能发生?
我猜它无法将 JSON 对象反序列化为参数类型,但我该如何解决这个问题?
编辑:添加代码
此方法有时会收到 value=null
public FilterItem Post(uint jobId, uint columnID, FilterItemDTO value)
{
// save a new FilterItem
}
FilterItemDTO 是我创建的一个 DTO 类,只是因为 FilterItem 类有一堆不需要的继承字段,我无法在基类中对它们进行注释:
public class FilterItemDTO
{
public uint Id;
public uint ColumnID { get; set; }
public FilterItem.FilterType Type;
public string Value;
//Date/Time Stamps
public DateTime? CreatedDate = null;
public string CreatedBy = null;
public DateTime? ModifiedDate;
public string ModifiedBy = null;
public int FilterLevel = 0;
public FilterItemDTO(FilterItem f)
{
Id = f.ID;
ColumnID = f.ColumnID;
Type = f.Type;
Value = f.Value;
FilterLevel = f.FilterLevel;
CreatedDate = f.CreatedDate;
CreatedBy = f.CreatedBy;
ModifiedDate = f.ModifiedDate;
ModifiedBy = f.ModifiedBy;
}
internal static FilterItem ToFilterItem(FilterItemDTO value)
{
FilterItem f = new FilterItem(value.Type, value.Value, value.FilterLevel);
f.ID = value.Id;
f.ColumnID = value.ColumnID;
f.CreatedDate = value.CreatedDate;
f.CreatedBy = value.CreatedBy;
f.ModifiedDate = value.ModifiedDate;
f.ModifiedBy = value.ModifiedBy;
return f;
}
public FilterItem ToFilterItem()
{
return FilterItemDTO.ToFilterItem(this);
}
}
这是我发送的请求(从 Fiddler 便笺簿复制)
POST http://localhost:56875/api/job/4513/column/21213/filter HTTP/1.1
Host: localhost:56875
Connection: keep-alive
Content-Length: 28
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:56875
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Content-Type: application/json
Referer: http://localhost:56875/Content/test.htm
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
{"type":"Equal","value":"1"}
这是我的格式化程序配置。type:"Equal" 值应该使用 StringEnumConverter 自动转换。
public class FormatterConfig
{
public static void RegisterGlobalFormatters(MediaTypeFormatterCollection formatters)
{
var jsonSerializerSettings = formatters.JsonFormatter.SerializerSettings;
jsonSerializerSettings.Converters.Add(new IsoDateTimeConverter());
// serialize every enum as a string
jsonSerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
// include null value fields
jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore;
// use camel case
jsonSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
// indented formatting
formatters.JsonFormatter.Indent = true;
formatters.Remove(formatters.XmlFormatter);
}
}