How to POST XML using Fiddler to ASP.NET WebAPI

后端 未结 4 826
夕颜
夕颜 2021-01-06 07:39

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 ser

相关标签:
4条回答
  • 2021-01-06 07:50

    You can mark your entity class with DataContract attribute setting namespace to an empty string:

    <DataContract(Namespace := "")>
    Public Class MyXmlData
    End Class
    

    After that xmlns parameters will not be necessary.

    0 讨论(0)
  • 2021-01-06 07:53

    For those who have the ability to do this when faced with the same issue, another option is to use JSON. I am a huge fan of XML, but unfortunately, it seems things are far more stacked against XML's usage in scenarios like this, because in this case, the standard WebApi parser requires all that hideous extra stuff, including intimate knowledge of backend namespaces and types, as well as a full XML spec namespace (* see rant below).

    {
      "SomeData": "R2D2",
      "UserName": "Johny",
      "Password": "password",
      "Num": 1013,  
      "IsCool": true
    }
    

    *( Rant, please ignore if you want to: Why require this, oh ye good gentleman who made WepApi and other such frameworks? why not allow the XML to be liberated, freely receivable without all this 'ugly'? If included, it could still specify something useful, if one needs to, but why must the rest of us include this ugliness? It seems things have been (arbitrarily, though perhaps more for historical baggage reasons, sorry you poor XML creature, you're stuck with yesterday's baggage though it need not be) stacked against XML's usage, including that, even with all this, a standard parser likely would not allow you to post the XML with the values as XML attributes. )

    0 讨论(0)
  • 2021-01-06 07:56

    Final XML snippet that was successfully deserialized:

    <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 is the name of the solution. That must've been what was missing.

    0 讨论(0)
  • 2021-01-06 07:57

    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>
    
    0 讨论(0)
提交回复
热议问题