我无法使用Xml而不是Json来使用Asp.Net Core Web Api项目。请帮忙!
我创建了一个新项目,对默认配置的唯一调整是添加Xml格式化程序。
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc(config =>
{
config.InputFormatters.Add(new XmlSerializerInputFormatter());
config.OutputFormatters.Add(new XmlSerializerOutputFormatter());
});
}
Run Code Online (Sandbox Code Playgroud)
我的控制器还包含简单的Get和Post方法:
[Route("api")]
public class MessageController : Controller
{
[HttpPost]
public void Post([FromBody] Message message)
{
}
[HttpGet]
public IActionResult Get()
{
return Ok(new Message
{
TestProperty = "Test value"
});
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用调用该POST方法时Content-Type: application/xml,API返回415不支持的媒体类型。我尝试将Consumes("application/xml")属性添加到控制器,但仍然无法正常工作。
GET工作并返回JSON。但是,如果我将Produces("application/xml")属性添加到控制器,即使我提供了Accepts: application/xml标头,GET也会返回406 Not Acceptable 。
由于某些原因,API完全拒绝了与xml相关的任何内容,即使我添加了输入和输出格式化程序,正如我在很少的示例中看到的那样。
我想念什么?