亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有 xml 輸入的 .net core 2.0 web api httppost 為 null

帶有 xml 輸入的 .net core 2.0 web api httppost 為 null

C#
慕哥9229398 2021-10-23 17:30:16
嘗試使用 .net core 2.0 web api HttpPost 方法來處理 xml 輸入。預期結果:當從 Postman 調用測試端點時,輸入參數(以下代碼中的 xmlMessage)應具有從 Postman HttpPost 正文發送的值。實際結果:輸入參數為空。在web api項目的startup.cs中,我們有如下代碼:public class Startup{   public Startup(IConfiguration configuration)   {      Configuration = configuration;   }   public IConfiguration Configuration { get; }   // This method gets called by the runtime. Use this method to add services to the container.   public void ConfigureServices(IServiceCollection services)   {      services.AddMvc()      .AddXmlDataContractSerializerFormatters();   }   // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.   public void Configure(IApplicationBuilder app, IHostingEnvironment env)   {      if (env.IsDevelopment())      {         app.UseDeveloperExceptionPage();      }      app.UseMvc();   }}在控制器中:[HttpPost, Route("test")]public async Task<IActionResult> Test([FromBody] XMLMessage xmlMessage){    return null; //not interested in the result for now}XMLMessage 類:[DataContract]public class XMLMessage{    public XMLMessage()    {    }    [DataMember]    public string MessageId { get; set; }}在郵遞員標題中:Content-Type:application/xmlHttp 帖子正文:<XMLMessage>  <MessageId>testId</MessageId></XMLMessage>感謝任何可以為我指明正確方向的幫助。提前致謝..
查看完整描述

3 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

您應該使用 XmlRoot/XmlElement 而不是 DataContract/DataElement 注釋類型。以下是應該更改以使其工作的內容。


在啟動.cs


public void ConfigureServices(IServiceCollection services){

    services.AddMvc(options =>

    {

        options.OutputFormatters.Add(new XmlSerializerOutputFormatter());

    });

    // Add remaining settings

}

XMLMessage 類:


[XmlRoot(ElementName = "XMLMessage")]

public class TestClass

{

    //XmlElement not mandatory, since property names are the same

    [XmlElement(ElementName = "MessageId")]

    public string MessageId { get; set; }

}

其他部分看起來不錯(控制器和標題)。



查看完整回答
反對 回復 2021-10-23
?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

我能夠讓它工作。我唯一需要改變的是方法Startup.ConfigureServices如下:


public void ConfigureServices(IServiceCollection services)

{

    services.AddMvc()

            .AddXmlSerializerFormatters(); 

}


查看完整回答
反對 回復 2021-10-23
  • 3 回答
  • 0 關注
  • 235 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號