Xml Serialization无法写入'x'类型的对象

Moh*_*hin 8 api asp.net-mvc xml-serialization web

我想使用XmlFormatter将类序列化为MVC Web API中的响应,但是在创建共振时我得到以下异常:

MediaTypeFormatter formatter = Configuration.Formatters.XmlFormatter;
HttpResponseMessage resp = Request.CreateResponse<Model>(HttpStatusCode.OK, value: modelObject, formatter: formatter);
Run Code Online (Sandbox Code Playgroud)

例外:

The configured formatter 'System.Web.Http.Tracing.Tracers.XmlMediaTypeFormatterTracer' cannot write an object of type 'Model'.
Run Code Online (Sandbox Code Playgroud)

怎么了 ?

Moh*_*hin 14

我在网上搜索了这个错误的任何线索,并在几小时后没有发现任何信息.

答案很简单.

Model类缺少默认的构造引起奇怪的非调试能够例外.

更多信息:为什么XML-Serializable类需要无参数构造函数