Xml Serialization cannot write an object of type 'x'

I want to serialize the class as an answer in the MVC web API using XmlFormatter, but when I create the response, I get the following exception:

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

An exception:

 The configured formatter 'System.Web.Http.Tracing.Tracers.XmlMediaTypeFormatterTracer' cannot write an object of type 'Model'. 

What's wrong?

+6
source share
1 answer

I dug up the Internet for any clue to this error and found nothing after a few hours.

The answer was simple.

The Model class did not have a default constructor that threw a strange exception that excluded debugging.

Additional information: Why XML-Serializable class needs a constructor without parameters

+13
source

Source: https://habr.com/ru/post/955833/


All Articles