ASP.NET MVC4 WebAPI and Posting XML data
asp.net-mvc, asp.net-mvc-4, asp.net-web-api, xml
Solution
The following will allow you to read a raw XML message via a POST to a Web API method:
public void PostRawXMLMessage(HttpRequestMessage request)
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(request.Content.ReadAsStreamAsync().Result);
}
You can debug and inspect the body, headers, etc. and will see the raw XML posted. I used Fiddler's Composer to make a HTTP POST and this works well.
Problem
I'm missing a trick with the new webapi - I'm trying to submit an xml string through a post request and not having much luck. The front end is using jQuery like this: ``` $(document = function () { $("#buttonTestAPI").click(function () { var d = " <customer><customer_id>1234</customer_id></customer>"; $.ajax({ type: 'POST', contentType: "text/xml", url: "@Url.Content("~/api/Customer/")", data: d, success: function (result) { var str = result; $("#output").html(str); } }); }); }); ``` My controller is pretty simple at the moment - just the default for the post action - trying to return what was passed in: ``` public string Post(string value) { return value; } ``` However, "value" is repeatedly null. The odd thing is, when I change my data in the jquery to be something like this: ``` d = "<customer_id>1234</customer_id>"; ``` Then I get "value" in my controller as 1234. How can I get access to the more complex xml string in my controller?