简体   繁体   中英

Posting XML to a WebAPI method always null

I am performing a systems integration. The client documentation states

"messages are transmitted with the HTTP CONTENT_TYPE header set to “application/x-www-form-urlencoded”.

<Message>
  <Originator>ACME Solutions</Originator>
  <Payload>
     ...message xml content goes here...
  </Payload>
</Message>

I am using WebApi 2 and the default controller action for post has the definition of

public void Post([FromBody]string value)

Everytime I post this message to my web service the value received is always null

I have tried creating a class with the same definition but that always has the properties as null.

public void Post([FromBody]Message value)

If I change the definition to a more generic one I can extract the Xml Document from the request content but thats not the way I want to go as it makes unit testing difficult.

public void Post(HttpRequestMessage request)

Can anyone suggest where I am going wrong and why the xml message isn't mapping to my class.

As you are setting the content type to application/x-www-form-urlencoded you also need to encode the body to match. So the body instead of looking like this:

<Message><Originator>ACME Solutions</Originator><Payload>message</Payload></Message>

Would look something like this:

value=%3CMessage%3E%3COriginator%3EACME+Solutions%3C%2FOriginator%3E%3CPayload%3Emessage%3C%2FPayload%3E%3C%2FMessage%3E

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM