[FromBody] Web parameter Api Asp.net arrives null if Text / xml is

1

hello I have the following problem with my web api I have an object

    public class Message
{
    public string Id { get; set; }
    public List<Resource> Contained { get; set; }
    public DateTimeOffset? Timestamp { get; set; }
    public Coding Event { get; set; }
    public MessageHeader.ResponseComponent Response { get; set; }
    public MessageHeader.MessageSourceComponent Source { get; set; }
    public List<MessageHeader.MessageDestinationComponent> Destination { get; set; }
}

which I receive by POST as follows

[Route("Update_Mensajestask/")]
    [HttpPost]
    public async Task<IHttpActionResult> Update_MensajestaskAsync( [FromBody]Message mensaje )
    {
        BusinessResult<int> bResult = null;
        try
        {
            _objNegocio = new Mensaje();
            bResult = await _objNegocio.UPDATE_MensajeRespuesta(mensaje).ConfigureAwait(false);
            if ( bResult.ResultType == ResultType.Failure )
            {
                return Ok(bResult.Message);

            }
            else
                return Ok(bResult.Result);
        }
        catch ( Exception )
        {

            throw;
        }

    }

but when I use the swagger service using parameter content type: application / xml or parameter content type: Text / xml the value of the object always arrives null, but if I use json, he recognizes me the object.

If someone can help me thank you in advance.

    
asked by Diego Marulanda 12.01.2018 в 15:10
source

1 answer

0

Try activating the XML formatter in the WebApiConfig file:

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

Or using XElement com parameter sending in content type as: content-type="application/xml" . In your action, it would be like this:

 [HttpPost]
 public async Task<IHttpActionResult> Update_MensajestaskAsync( [FromBody]XElement mensaje)

The only drawback with this method is that you will have to convert the object XElement to Message manually. While activating the XML Fomatter has the problem that is very strict when serializing since a simple space or carriage return that can not interpret in the xml can throw you an incomprehensible error. It would be your decision which one to use.

    
answered by 12.01.2018 / 15:20
source