Issue with sending XML to Web Api via http Post request

前端 未结 2 863
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 07:26

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 bo

相关标签:
2条回答
  • 2020-12-18 08:03

    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).

    0 讨论(0)
  • 2020-12-18 08:10

    You can configure Web API to use XmlSerializer in your WebApiConfig.Register as follows;

    config.Formatters.XmlFormatter.UseXmlSerializer = true;
    
    0 讨论(0)
提交回复
热议问题