我想将以下XML文档发送到我的Web Api 2控制器;但是,[FromBody]
参数始终为空。
这是请求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>
使用Postman发送请求,如下:
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控制器:
注意:param
参数始终为null
我相信我需要一个合适的类定义才能正确映射XML请求,但我不确定如何构建它。
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作为参数。
[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;
}
答案 0 :(得分:4)
最终解决方案是将Post param类型更改为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,无论如何,谢谢你的意见)。
答案 1 :(得分:3)
您可以将XmlSerializer
配置为使用WebApiConfig.Register
的Web API,如下所示;
config.Formatters.XmlFormatter.UseXmlSerializer = true;