Xml请求不起作用

fra*_*o87 4 c# asp.net-core asp.net-core-webapi

我无法使用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相关的任何内容,即使我添加了输入和输出格式化程序,正如我在很少的示例中看到的那样。

我想念什么?

dot*_*tep 5

我在startup.cs中有以下内容,它与XML和JSON都可以很好地工作。在这里,我只坚持使用XML。注意:(我考虑了自己的班级作为示例)

  1. 启动文件

    public void ConfigureServices(IServiceCollection services)
        { 
            services.AddMvcCore()
                    .AddJsonFormatters().AddXmlSerializerFormatters();
        }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 我的HttpClient代码(您可能已经错过了我在StringCotent中完成的“内容类型”设置)

    • 两个标头很重要:Accept和Content-Type。在内容协商中接受帮助,Content-Type是客户端告诉服务器内容客户端发布的类型的一种方式。

       HttpClient client = new HttpClient();
       client.BaseAddress = new Uri( @"http://localhost:5000");
       client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));
      
      
       HttpContent content = new StringContent(@"<Product>
      <Id>122</Id>
      <Name>Computer112</Name></Product>",System.Text.Encoding.UTF8 , "application/xml");  // This is important.
      
      var result = client.PostAsync("/api/Products", content).Result;
      
      Run Code Online (Sandbox Code Playgroud)


Mik*_*Mik 5

在 ASP.Net Core 2.0 中,您几乎可以开箱即用地接受 XML 和 Json 请求。

\n\n

在ConfigureServices方法的Startup类中,您应该具有:

\n\n
services\n    .AddMvc()\n    .AddXmlSerializerFormatters();\n
Run Code Online (Sandbox Code Playgroud)\n\n

接受复杂对象的控制器如下所示:

\n\n
[Route("api/Documents")]\npublic class DocumentsController : Controller\n{\n    [Route("SendDocument")]\n    [HttpPost]\n    public ActionResult SendDocument([FromBody]DocumentDto document)\n    {\n        return Ok();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是要发送的 XML:

\n\n
<document>\n    <id>123456</id>\n<content>This is document that I posted...</content>\n<author>Micha\xc5\x82 Bia\xc5\x82ecki</author>\n<links>\n    <link>2345</link>\n    <link>5678</link>\n</links>\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n\n

{\n    id: "1234",\n    content: "This is document that I posted...",\n    author: "Micha\xc5\x82 Bia\xc5\x82ecki",\n    links: {\n        link: ["1234", "5678"]\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

那\xe2\x80\x99就是它!它就是有效的。

\n\n

.net核心调试

\n\n

使用 XML 或 Json 格式向 api/documents/SendDocument 端点发送相同文档的请求由一种方法处理。只需记住请求中正确的 Content-Type 标头即可。

\n\n

您可以在我的博客上阅读整篇文章:http://www.michalbialecki.com/2018/04/25/accept-xml-request-in-asp-net-mvc-controller/

\n