我想使用 XmlFormatter 在 MVC Web API 中序列化一个类作为响应,但在创建响应时出现以下异常:
MediaTypeFormatter formatter = Configuration.Formatters.XmlFormatter;
HttpResponseMessage resp = Request.CreateResponse<Model>(HttpStatusCode.OK, value: modelObject, formatter: formatter);
异常(exception):
The configured formatter 'System.Web.Http.Tracing.Tracers.XmlMediaTypeFormatterTracer' cannot write an object of type 'Model'.
出了什么问题?
最佳答案
我在网上挖掘了有关此错误的任何线索,但几个小时后什么也没发现。
答案很简单。
Model
类缺少默认构造函数,这导致了奇怪的不可调试异常。
更多信息:Why XML-Serializable class need a parameterless constructor
关于asp.net-mvc - Xml序列化无法写入 'x'类型的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19332800/