通过http Post请求将XML发送到Web Api的问题 [英] Issue with sending XML to Web Api via http Post request

查看:208
本文介绍了通过http Post请求将XML发送到Web Api的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下XML文档发送到Web Api 2控制器;但是,[FromBody]参数始终为空.

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

这是请求XML正文:

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

使用邮递员发送请求,如下所示:

Using Postman to send the request, is as follows :

POST /api/razorXmlRequest HTTP/1.1
Host: localhost:5000
Content-Type: application/xml
Cache-Control: no-cache
Postman-Token: 6ca91ebf-31e0-77f2-6f81-cb9993b69b1a

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

还有Web Api 2控制器:

And the Web Api 2 controller :

注意:param参数始终为null

我相信我需要一个正确的类定义来正确映射XML请求,但是我不确定如何构造它.

I believe I need a proper class definition to correctly map the XML request, but I'm not sure how to construct it.

using System;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using RazorServices;
using System.Net.Http;
using System.Web.Http;
using System.Xml.Linq;
using System.Net;
using System.Xml;

namespace RazorWebApi.Controllers
{
    [Route("api/razorXmlRequest")]
    public class RazorXmlRequestController : ApiController
    {
        public class RawXml {
            public string RazorXml { get; set; }
        }

        [HttpPost]
        public async Task<HttpResponseMessage> Post([FromBody]RawXml param )
        {
            HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK);

            // code omitted...
			
            return resp;
        }
    }
}

顺便说一下,我们目前正在通过设置StreamReader对象从this.Request.Content中提取来解决此问题;但是在单元测试中,我需要将xml作为参数发送.

And by the way, we are currently getting around this issue by setting up a StreamReader object to pull from this.Request.Content; however in my unit test I need to send my xml as a parameter.

[HttpPost]
        public async Task<HttpResponseMessage> Post()   // [FromBody]RawXml param )
        {
            HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK);

            StreamReader sr = new StreamReader(await this.Request.Content.ReadAsStreamAsync(), Encoding.UTF8);
            string xml = sr.ReadToEnd();

            if (xml == null || xml.Length < 4)
            {
                throw new RazorServicesException("Invalid XML");
            }
            RazorSession cred = RzWebApi.Cred;
            string reply = await RzWebApi.RazorSrv.xmlRequests.Request(cred, xml);
            resp.Content = new StringContent(reply, System.Text.Encoding.UTF8, "text/plain");
            return resp;
        }

推荐答案

最终解决方案是将Post参数类型更改为XElement:

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;
    }
}

此方法直接工作,甚至没有提供以上先生们建议的XmlFormatter(@TusharJ,无论如何,谢谢您的输入).

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

这篇关于通过http Post请求将XML发送到Web Api的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆