1

I am working a asp.net core 6.0 project.

when sending an API, The response of API has all fields until controller's last line. But in postman some Fields are missing

Example : Here ticketed is missing in postman response. But it is in controller. I attached the images below.

Screen shot in controller

Postman response

Note : I added NewtonsoftJson nuget package <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.1" />

in Program.cs

builder.Services.AddControllers(options =>
                options.Filters.Add<ApiExceptionFilterAttribute>())
                    .AddFluentValidation()
.AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);


Edit:

AirPriceVM.cs

public class AirPriceVM
{
    public string TraceId { get; set; }

    public Data Data { get; set; }
}
public class Data
{

    public IEnumerable<AirPricePricingSolutionVm> AirPricingSolution { get; set; }

}

public class AirPricePricingSolutionVm
{
    public string Key { get; set; }

    public IEnumerable<AirPricePricingInfoVm> AirPricingInfo { get; set; }


}


public class AirPricePricingInfoVm
{
    public bool Ticketed { get; set; }

    public bool TicketedSpecified { get; set; }
}


// I removed some properties

in Controller

    [HttpPost]
    [Route("price")]
    public async Task<ActionResult<AirPriceVM>> AirPrice([FromQuery] string siteCode, [FromQuery] string marketingMedium, [FromBody] GetAirPriceQuery dto)
    {
        dto.SiteCode = siteCode;
        dto.MarketingMedium = marketingMedium;
        var result = await Mediator.Send(dto);

        return Ok(result);   // until this line API response has "ticketed" . but in postman "ticketed" is missing
    }

What did I miss here? Please anyone help me to find the mistake.

Thank you

hanushi
  • 1,169
  • 2
  • 12
  • 27
  • Have you checked the type of `result`, From the images you provided, I suspect they are not the same type – Xinran Shen Jun 29 '22 at 02:54
  • @XinranShen Yes the result are same, In debug mode they are ordered in alphabet but in postman they are ordered like model. – hanushi Jun 29 '22 at 02:58
  • @XinranShen See the filed named `ticketed`. I searched in postman But result is not there. – hanushi Jun 29 '22 at 02:59
  • the type of `result` is `AirPriceVM`? But i don't find any properties named `SiteCode` and `MarketingMedium` in your `AirPriceVM` model, Can you share your `IRequest<>` class? – Xinran Shen Jun 29 '22 at 03:04
  • `AirPriceVM` is return type. `SiteCode` is used as an input. `SiteCode` is used for Query`GetAirPriceQuery` – hanushi Jun 29 '22 at 03:11
  • 1
    It's because of your `TicketedSpecified` property. When `TicketedSpecified` is false, `Ticketed` is not serialized. If you have property named `xxx` and also a property with the exact name `bool xxxSpecified`, then the bool property will control whether the `xxx` property is serialized. See [How to force Newtonsoft Json to serialize all properties? (Strange behavior with "Specified" property)](https://stackoverflow.com/q/39223335/3744182). In fact that may be a duplicate, agree? – dbc Jun 29 '22 at 03:23
  • @dbc could you please check this question https://stackoverflow.com/questions/72819861/system-formatexception-an-error-occurred-while-deserializing-the-xxx-property-o – hanushi Jul 01 '22 at 02:34

0 Answers0