Home > database >  Part of JSON object lost over HttpClient/IActionResult exchange in .net6/webAPI
Part of JSON object lost over HttpClient/IActionResult exchange in .net6/webAPI

Time:01-04

I have a BlazorWASM client requesting a response from my API using HttpClient in .Net5/6.

The object 'DataManifest' is populated then submitted through IActionResult via the OK() method:

   [HttpGet]
    [Route("/Data/GetManifest")]
    public async Task<IActionResult> GetManifest()
    {
        try
        {
            DataManifest manifest = new();
            manifest.LastJob = await _db.jobs.MaxAsync(x=>x.id);
            manifest.LastJobCost = await _db.jobcosts.MaxAsync(x => x.id);
            manifest.LastEvent = await _db.events.MaxAsync(x => x.task_id);
            manifest.LastPurchaseInvoice = await _db.purchaseinvoices.MaxAsync(x => x.id);
            manifest.LastTimeRecord = await _db.TimeRecords.MaxAsync(x => x.id);
  
            return Ok(new ApiResponse<DataManifest> { IsSuccess = true, Message = "", Value = manifest, msTiming = (int)watch.ElapsedMilliseconds });

examining the object in VS shows it is intact with data included, the properties of 'manifest' being set correctly.

However looking at what is contained in the http response (status:200), the value/'manifest' object is null (the other fields being populated as expected) from DevTools in Edge:

{value: {}, message: "", isSuccess: true, msTiming: 17}

I have other HttpGet's that are working perfectly (indeed with objects like ApiResponse<List>). I can't see why my DataManifest object is causing an issue!

--- Further info: ApiResponse is:

 public class ApiResponse
{
    public string Message { get; set; } = default!;
    public bool IsSuccess { get; set; }
    public int msTiming { get; set; }

}

public class ApiResponse<T> : ApiResponse
{
    public T Value { get; set; } = default!;
}

Manifest:

 public class DataManifest
{
    public int lastJob;
    public int LastEvent;
    public int LastJobCost;
    public int LastPurchaseInvoice;
    public int LastTimeRecord;
}

CodePudding user response:

As was solved through comments, the issue here was with the DataManifest declaration having only fields.

By default, System.Text.Json ignores fields. You can manually configure it to include them, either through options or through attributes on the fields, but the fields can also be turned into properties to make it work.

Basically, change this:

public class DataManifest
{
    public int lastJob;
    public int LastEvent;
    public int LastJobCost;
    public int LastPurchaseInvoice;
    public int LastTimeRecord;
}

to this:

public class DataManifest
{
    public int lastJob { get; set; }
    public int LastEvent { get; set; }
    public int LastJobCost { get; set; }
    public int LastPurchaseInvoice { get; set; }
    public int LastTimeRecord { get; set; }
}

CodePudding user response:

Can you check you don't have a JsonIgnore attribute on Value field definition?

  • Related