Ich möchte das folgende XML-Dokument an meine Web Api 2-Controller senden; Der Parameter ist jedoch immer null.Problem mit dem Senden von XML an Web Api über http Post Anfrage
Hier ist die Anfrage XML Körper:
<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 Mit der Anfrage zu senden, ist wie folgt:
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>
Und der Web-Api 2-Controller:
HINWEIS: Der param
Parameter ist immer null
Ich glaube, ich eine richtige Klassendefinition muß korrekt die XML-Anforderung auf der Karte, aber ich bin nicht sicher, wie es zu konstruieren.
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...
\t \t \t
return resp;
}
}
}
Und übrigens, sind wir derzeit, um dieses Problem immer durch die Einrichtung eines StreamReader
Objekt aus this.Request.Content
zu ziehen; In meinem Komponententest muss ich jedoch meine XML als Parameter senden.
[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;
}
Es stellt sich heraus, dass ich die Post-Methode zu 'Post ([FromBody] XElement param)' und die Überschrift 'Content-Type: application/xml' ändern kann, und dies wird das Problem lösen. Der 'xml'-Inhalt wird auch ohne den obigen Vorschlag an den Controller übergeben. –