asp.net-web-api2 – 通过http Post请求将XML发送到Web Api的问
发布时间:2020-12-16 09:12:54 所属栏目:asp.Net 来源:网络整理
导读:我想将以下 XML文档发送到我的Web Api 2控制器;但是,[FromBody]参数始终为null. 这是请求XML主体: razorInbound server="PortfolioExposureServer" request="lookupRiskPointRecord" callerRazorClient/caller responseModexml/responseMode body condition
我想将以下
XML文档发送到我的Web Api 2控制器;但是,[FromBody]参数始终为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> 使用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对象来解决这个问题.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 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,"application/xml"); return resp; } } 这是直接工作,甚至没有像上面提到的那样提供XmlFormatter(@TusharJ,非常感谢你的输入). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 检测asp.net将用户重定向到登录页面
- asp.net-mvc-3 – Asp.Net MVC 3自定义WebViewPage在不同的
- 实体框架 – MVC 3 EF 4.1 dbContext – 删除具有不可空的外
- asp.net-mvc – 具有适当的敲除绑定的网格小部件
- asp.net-web-api – 如何在ASP.NET Web API中验证文件上载
- asp.net – Page_Load中的Response.Redirect
- asp.net-mvc – ASP.NET MVC ActionResult背后的推理是一个
- asp.net-mvc – 在ASP.NET MVC 3中授权当前用户对控制器和操
- asp.net – 新安装的IIS. Aspx页面不会显示
- asp.net-mvc-3 – 如何创建通用的MVC3编辑器模板?
推荐文章
站长推荐
- asp.net-mvc – N2 for MVC – 如何让Zones工作?
- SQL Server2005探索之—— 利用SQL Server2005提
- asp.net-mvc – 使用Ajax.Beginform的RedirectTo
- asp.net-mvc – 如何设置内联的webgrid行样式
- asp.net – 本地化mvc中的默认模型验证2
- 如何在asp.net中打开MS Office word?
- asp.net-mvc-4 – DD4T默认路由不起作用
- asp.net – XmlSerializer可以反序列化为Nullabl
- asp.net-mvc-3 – 我的路由如何使用ASP MVC3在UR
- .NET Core技术研究-最实用最常用的配置读取方式
热点阅读