2016-05-24 16 views
1

Web Api 2コントローラに次のXML文書を送信します。ただし、[FromBody]パラメータは常にnullです。 、リクエストを送信するために郵便配達を使用しhttpを経由してWeb APIに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>

次のとおりです:ここで

は、要求XML体である

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パラメータは、私は私が正しくXML要求をマッピングするために、適切なクラス定義を必要とすると信じて、常にnull

ですが、私はそれを構築するかどうかはわかりません。

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

ところで、私たちは現在、this.Request.ContentからプルするStreamReaderオブジェクトを設定することによってこの問題を回避なっています。私の単体テストでは、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; 
 
     }

答えて

1

最終的な解決策はXElementにポストのparamタイプを変更することでした:

[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

あなたWebApiConfig.RegisterでのXmlSerializerを使用するようにWeb APIを構成することができます。

config.Formatters.XmlFormatter.UseXmlSerializer =はtrue。

+0

Postメソッドを 'Post([FromBody] XElement param)'とヘッダーの 'Content-Type:application/xml'に変更することができます。これで問題は解決します。上記の提案がなくても、 'xml'コンテンツはコントローラに渡されます。 –

関連する問題