如何使用招到ASP.NET的WebAPI到XML POST [英] How to POST XML using Fiddler to ASP.NET WebAPI
本文介绍了如何使用招到ASP.NET的WebAPI到XML POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于以下ASP.NET的WebAPI,我试图发送使用招测试POST,但不能让它开始工作。无论我送,我总是只看到的没有数据发送到服务的消息
Given the following ASP.NET WebAPI, I am trying to send a test POST using Fiddler, but can't get it to work. Whatever I send, I always just see the No data sent to 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:
任何人都可以请告知我在做什么错?我用内容类型:文本/ XML
希望ASP.NET将能够正确地解读它。
Can anyone please advise on what I'm doing wrong? I used Content-Type: text/xml
hoping that ASP.NET would be able to decipher it correctly.
更新
以下输入新的截屏:
推荐答案
请在内容类型:文本/ XML
请求头并更改XML喜欢这个
Keep the Content-Type: text/xml
request header and change the XML to like this.
<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>
这篇关于如何使用招到ASP.NET的WebAPI到XML POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文