gpt4 book ai didi

asp.net-web-api2 - 通过 http Post 请求将 XML 发送到 Web Api 的问题

转载 作者:行者123 更新时间:2023-12-04 18:03:34 25 4
gpt4 key购买 nike

我想将以下 XML 文档发送到我的 Web Api 2 Controller ;然而,[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 Controller :

注意: 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;
}

最佳答案

最终的解决方案是将 Post 参数类型更改为 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,无论如何感谢您的投入)。

关于asp.net-web-api2 - 通过 http Post 请求将 XML 发送到 Web Api 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37424559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com