簡體   English   中英

將 Json 服務添加到我的 ASP.Net Web Api 會導致 Postman 中的響應格式發生變化

[英]Adding a Json service to my ASP.Net Web Api causes a change in the format of the response in Postman

在沒有特定 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 不會是 Json 的響應(我真正鄙視的那個):

<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 響應?

AddXmlDataContractSerializerFormatters()是問題...

改成:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM