• Web API XML the model is always null in ASP.net

    Abhey Member

    sending XML to Web API method using the POST method but Web API XML the model is always null in ASP.net

  • Ganesh Member

    Are you checking for Model binding errors? stick this at the beginning of your controller methods:

    if (!ModelState.IsValid)
    {
         return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
    }

    Deserialise the stream yourself
    In your controller, can you get the raw content of the request and try to deserialise it yourself? This will also help. (this is just for troubleshooting and should not be how you deal with the deserialisation)

    try
    {
        string data = Request.Content.ReadAsStringAsync() ;
        new XmlSerializer(typeof(yourType)).Deserialize(new StringReader(data));
    } catch(Exception ex)
    {
         Debug.WriteLine(ex.Tostring);
    }
    
  • Abhey Member

    it was an issue in Model binding and expecting XML namespace information. Yes I am able to fix the issue,but it’s using DataContractSeralizer instead XmlSerializer.

    I have already configured GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true; in Startup.cs.

  • Ganesh Member

    If this is set GlobalConfiguration . Configuration . Formatters.XmlFormatter.UseXmlSerializer = true, then I’m afraid i cannot think of any other reasons why there should still be problems. Is there a specific error message? I cannot think of anything else though…

  • SapnaVishwas Member

    It was a problem with model mapping. on API side few request type properties are XML attributes and when it’s mapped with other class type using automapper, it looks unable to map it correctly. Now i have made all request type properties as XML elements and mapped it, now no media type error and works fine.

Viewing 4 reply threads
  • You must be logged in to reply to this topic.