将Json服务添加到API会导致Postman中响应的格式发生更改

kmpatx3s  于 2023-01-03  发布在  Postman
关注(0)|答案(1)|浏览(143)

在没有特定Json服务的Program.cs中:

builder.Services.AddControllers(options =>
{
    options.ReturnHttpNotAcceptable = true;
}).AddXmlDataContractSerializerFormatters();

Postman Json回应(我更喜欢的一个):

{
    "id": 1,
    "name": "Central Park",
    "description": "The most visited urban park in the United States."
}

在具有特定Json服务的Program.cs中:

builder.Services.AddControllers(options =>
{
    options.ReturnHttpNotAcceptable = true;
}).AddXmlDataContractSerializerFormatters().AddNewtonsoftJson();

Postman 的React,不会是杰森(一个我真的鄙视):

<PointsOfInterestDto xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/CityInfo.API.Models"><Description>The most visited urban park in the United States.</Description><Id>1</Id><Name>Central Park</Name></PointsOfInterestDto>

如何实现Json响应?

iugsix8n

iugsix8n1#

AddXmlDataContractSerializerFormatters()是个问题...
变更为:

builder.Services.AddControllers(options =>
{
    options.ReturnHttpNotAcceptable = true;
}).AddNewtonsoftJson();

相关问题