Using Fiddler I can pass in the body
someXml=ThisShouldBeXml
and then in the controller
[HttpPost]
public ActionResult Test(object someXml)
{
return Json(someXml);
}
gets this data as a string
How do I get fiddler to pass XML to the MVC ActionController ? If I try setting the value in the body as raw xml it does not work..
And for bonus points how do I do this from VBscript/Classic ASP?
I currently have
DataToSend = "name=JohnSmith"
Dim xml
Set xml = server.Createobject("MSXML2.ServerXMLHTTP")
xml.Open "POST", _
"http://localhost:1303/Home/Test", _
False
xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xml.send DataToSend
In order to pass the data as a sting in MVC you have to create your own media type formatter to handle plain text. Then add the formatter to the config section.
To use the new formatter specify the Content-Type for that formatter, like text/plain.
Sample formatter for text
Controller method:
Setup in the WebApiConfig.cs Register method:
Fiddler headers:
MVC Controller is not ideal for such request handling, but that was the task, so let’s get to it. Let's have an XML that I to accept:
I tried a few solutions with built-in parameter deserialization but none seem to work and finally, I went with deserializing a request in a method body. I created a helper generic class for it:
I decorated my DTO with xml attributes:
And used all of that in a controller:
To use it, just send a request using for example Postman. I’m sending POST request to http://yourdomain.com/documents/sendDocument endpoint with xml body mentioned above. One detail worth mentioning is a header. Add Content-Type: text/xml, or request to work.
And it works:
You can see the whole post on my blog: http://www.michalbialecki.com/2018/04/25/accept-xml-request-in-asp-net-mvc-controller/
In order to send the request using VBScript I have used the WinHttp object i.e. "WinHttp.WinHttpRequest.5.1".
Below is a function which I wrote and this sends the XML request that you pass in and returns the response:
You cannot directly pass XML data as file to MVC controller. One of the best method is to pass XML data as Stream with HTTP post.
For Posting XML,
Refer to this stackoverflow post for more details about posting XML to MVC Controller
For retrieving XML in the controller, use the following method
This seems to be the way to pay XML to a MVC Controller
How to pass XML as POST to an ActionResult in ASP MVC .NET
I tried to get this to work with WEB API but could not so I have to used the MVC 'Controller' instead.