ASP.NET Core 2.2,XML 的输入格式器

ASP.NET Core 2.2, input formatters for XML

我正在使用 asp.net 核心 2.2 并尝试为 XML 允许额外的内容类型值。所以我在 Startup.cs 中这样配置。

services.AddMvc(options =>
        {
            options.ReturnHttpNotAcceptable = true;
            options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
            options.InputFormatters.Add(new XmlSerializerInputFormatter(options));
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2); 

事实上,我最终获得了邮递员的 415 Unsupported Media Type 身份。有没有人成功添加 XML content-type 值?

AuthorsController.cs:

    [HttpPost]
    public ActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
    {
        ...
    }

您需要将 RespectBrowserAcceptHeader 设置为 true 以允许内容协商。

// Add XML Content Negotiation
config.RespectBrowserAcceptHeader = true;


services.AddMvc(options => {
    //Enable this to allow content negotiation.
    options.RespectBrowserAcceptHeader = true;

    options.ReturnHttpNotAcceptable = true;
    options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
    options.InputFormatters.Add(new XmlSerializerInputFormatter(options));
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);