尝试使用 .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/xml
Http 帖子正文:
<XMLMessage>
<MessageId>testId</MessageId>
</XMLMessage>
感谢任何可以为我指明正确方向的帮助。提前致谢..
撒科打诨
宝慕林4294392
相关分类