Given the following ASP.NET WebAPI, I am trying to send a test POST using Fiddler but cannot make it work. No matter what I send, I always see a "No" message sent to a service message.
Imports System.Web.Http Imports System.Net.Http Imports System.Net Namespace HelloWebApiDemo Public Class MyApiController Inherits ApiController Public Function [Get]() As HttpResponseMessage Return Request.CreateResponse(HttpStatusCode.OK, "Hello") End Function Public Class MyXmlData Public Property UserName As String Public Property Password As String Public Property SomeData As String End Class Public Function Post(<FromBody> x As MyXmlData) As HttpResponseMessage If x Is Nothing Then Return Request.CreateResponse(HttpStatusCode.InternalServerError, "No data sent to service") End If Dim u As String = String.Empty Dim p As String = String.Empty Dim d As String = String.Empty If Not String.IsNullOrEmpty(x.UserName) Then u = x.UserName End If If Not String.IsNullOrEmpty(x.Password) Then p = x.Password End If If Not String.IsNullOrEmpty(x.SomeData) Then d = x.SomeData End If Return Request.CreateResponse(HttpStatusCode.OK, String.Format("You posted {0}, {1} with a string that is {2} characters in length", u, p, d.Length.ToString)) End Function End Class End Namespace
In Fiddler, my POST looks like this:
Can anyone advise what I am doing wrong? I used it Content-Type: text/xml, hoping ASP.NET could decrypt it correctly.
Content-Type: text/xml
Update
New screen capture with the following input:
Keep the request header Content-Type: text/xmland change the XML in this way.
<MyApiController.MyXmlData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/HelloWebApiDemo.HelloWebApiDemo"> <Password>somepassword</Password> <SomeData>somedata</SomeData> <UserName>bob</UserName> </MyApiController.MyXmlData>
XML, :
<MyApiController.MyXmlData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/_WebApplication1.HelloWebApiDemo"> <Password>Password</Password> <SomeData>Data here</SomeData> <UserName>Some Username</UserName> </MyApiController.MyXmlData>
_WebApplication1 - . , , , .
_WebApplication1
, , , - JSON. XML, , , , , XML, WebApi , , , XML (* . rant ).
{ "SomeData": "R2D2", "UserName": "Johny", "Password": "password", "Num": 1013, "IsCool": true }
* (Rant, , , : , , WepApi ? XML , ""?, - , , ? , (, , , , XML-, , ) XML, , , , XML XML.)
You can mark an entity class using the DataContract attribute namespace for an empty string:
<DataContract(Namespace := "")> Public Class MyXmlData End Class
After that, xmlns options will not be needed.