Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

asp.net web api2 - Issue with sending XML to Web Api via http Post request

I would like to send the following XML document to my Web Api 2 controller; however, the [FromBody] parameter is always null.

Here is the request XML body:

<razorInbound server="PortfolioExposureServer" request="lookupRiskPointRecord">
  <caller>RazorClient</caller>
  <responseMode>xml</responseMode>
  <body>
    <conditions>
      <fromOffset>0</fromOffset>
      <top>100</top>
      <condition>
        <keyPath>
          <keyElement nodeOffset='1'>Currency</keyElement>
          <keyElement nodeOffset='2'>ID</keyElement>
        </keyPath>
        <lookupValue>USD</lookupValue>
      </condition>
    </conditions>
  </body>
</razorInbound>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Final solution was to change the Post param type to XElement:

[Route("api/razorXmlRequest")]
public class RazorXmlRequestController : ApiController
{
    /// <summary>
    /// Raw Razor request XML 
    /// </summary>
    /// <returns>Razor reply message as text</returns>
    [HttpPost]
    public async Task<HttpResponseMessage> Post([FromBody]XElement xml)
    {
        HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK);
        RazorSession cred = RzWebApi.Cred;
        string reply = await RzWebApi.RazorSrv.xmlRequests.Request(cred, xml);
        resp.Content = new StringContent(reply, System.Text.Encoding.UTF8, "application/xml");
        return resp;
    }
}

This worked straight-away without even providing the XmlFormatter as the gentlemen had suggested above (@TusharJ, thank you anyway for your input).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...