0

I want to send xmldata via fiddler to my wepabi, but my webapi doesn't get the data for some reason. I want to know the reason;)

What I have:

[RoutePrefix("Document")]
public class DocumentController : ApiController
{
    [HttpPost]
    [Route("AddDocument")]
    public IHttpActionResult AddDocument([FromBody] XmlDocument doc)
    {
        //  Do Stuff
        return Ok();
    }
}

What my fiddler looks like FiddlerRequest

When I do this request doc is always null. What am I doing wrong?

1 Answer 1

1

In order to use content negotiation you need to accept a class which the model binder can bind to in your controller. Like this:

public class Document
{
    public int Id { get; set; }
    public string BaseUrl { get; set; }
    public string Name { get; set; }
    public int Active { get; set; }
    public Page[] Pages { get; set; }
}

public class Page
{
    public int Id { get; set; }
    public string Url { get; set; }
    public string InternalId { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
}

Then you accept this as your argument instead of a XmlDocument:

[RoutePrefix("Document")]
public class DocumentController : ApiController
{
    [HttpPost]
    [Route("AddDocument")]
    public IHttpActionResult AddDocument([FromBody] Document doc)
    {
        //  Do Stuff
        return Ok();
    }
}

And now you can change the Content-Type header in your request to be either application/xml, text/xml or application/json depending on the format being posted.

Sign up to request clarification or add additional context in comments.

3 Comments

Your solutions works when I change the xmlns in my request. I dont really want to change that, but then I get the following error: Expecting element 'Document' from namespace 'http://schemas.datacontract.org/2004/07/NoteStop.Models'.. Encountered 'Element' with name 'Document', namespace 'http://www.lias.nl/schemas/notestop-document' Is there a way to change the notestop.models namespace?
You can annotate your Document class with the XmlRoot attribute and specify your namespace. [XmlRoot(Namespace = "http://www.lias.nl/schemas/notestop-document")]
Thank you for the reply, but sadly it did not work. Got the same error. For now I give the HttpRequestMessage request as a parameter and extreact the xml from the content. But not ideal...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.