question

StefanoMilanesi-8258 avatar image
0 Votes"
StefanoMilanesi-8258 asked miwan2-msft edited

ASP.NET Core REST Api - how to return multiple objects from GET method

Hi,

I need to return multiple objects from GET method:

     [HttpGet]
     [ActionName("GetTimesheets")]
     [AllowAnonymous]
     public (List<ListTimesheets>, Errors) GetTimesheets([FromBody] TimeSheetBO timeSheetBO)
     {
         (List<ListTimesheets> ListTimesheetsTmp, Errors ErrorTmp) = TimesheetDAL.GetUserTimesheets(timeSheetBO);
         return (ListTimesheetsTmp, ErrorTmp);
     }

from Blazor page I cannot receive the correct values:

     var request = new HttpRequestMessage
     {
         Method = HttpMethod.Get,
         RequestUri = new Uri(@MySettings["URLTimeReportRestAPI"] + "/ControllerName/GetTimesheets"),
         Content = new StringContent(JsonConvert.SerializeObject(ObjTimeSheet), System.Text.Encoding.UTF8, "application/json"),

     };
     (var ListTimesheetsTmp, var ErrorTmp) = await http.SendJsonAsync<(List<ListTimesheets>, Errors)>(request.Method, request.RequestUri.ToString(), ObjTimeSheet);


both objects (ListTimesheetsTmp, ErrorTmp) are NULL.

Any idea?

Best
Stefano

dotnet-aspnet-core-blazor
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, @StefanoMilanesi, Is timeSheetBO null from the controller? You could debug and check the value of [FromBody] TimeSheetBO timeSheetBO first.

0 Votes 0 ·
miwan2-msft avatar image
0 Votes"
miwan2-msft answered miwan2-msft edited
 <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />  for Asp.Net Core 2.1

The main result you failed to deserialize is you didn't return json object correctly.
You could create a combind object which contains ListTimesheetsTmp and ErrorTmp. And deserialize with the same Obj.

     public class Obj
     {
         public List<ListTimesheets> listTimesheetsTmp;
         public Errors errorTmp;
    
         public Obj(List<ListTimesheets> listTimesheetsTmp, Errors errorTmp)
         {
             this.listTimesheetsTmp = listTimesheetsTmp;
             this.errorTmp = errorTmp;
         }
     }

Test
![78755-image.png




image.png (11.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

StefanoMilanesi-8258 avatar image
0 Votes"
StefanoMilanesi-8258 answered

Hi,
first of all, thank you for your reply.

I have tested the procedure/service/controller several times, in different way (postman, trace log, ...) and I can say that:

  • thimesheetBO object is correctly valued. It contains the search criteria fields that the user has filled

  • TimesheetDAL.GetUserTimesheets function, returned correctly a list of timesheets (List<ListTimesheets>) and the Error object, for testing purpose, has been filled with testing values (test messages) ... so, in the controller, the two objects are correctly filled

The problem is when they (two objects) are returned to the main call on the Blazor page, both contains null values

Thanks
Stefano



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

miwan2-msft avatar image
0 Votes"
miwan2-msft answered

Hi, @StefanoMilanesi-8258

Use Newtonsoft’s Json.NET instead of System.Text.Json. System.Text.Json is more strict than NewtonsoftJson.

How to replace?

Step 1. Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

78159-image.png

Step 2. services.AddRazorPages().AddNewtonsoftJson();

Adding the AddNewtonsoftJson() call to the end means that we’re going to use the Newtonsoft.Json APIs over the default System.Text.Json implementation.

Then, change the codes of controller like below

         [HttpGet]
         [ActionName("GetTimesheets")]
         [AllowAnonymous]
         public async Task<IActionResult> GetTimesheets([FromBody] TimeSheetBO timeSheetBO)
         {
             (List<ListTimesheets> ListTimesheetsTmp, Errors ErrorTmp) = TimesheetDAL.GetUserTimesheets(timeSheetBO);
             return Json((ListTimesheetsTmp, ErrorTmp));
         }

Test result

78166-image.png


If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Best Regards,
Michael Wang



image.png (14.0 KiB)
image.png (32.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

miwan2-msft avatar image
0 Votes"
miwan2-msft answered miwan2-msft published

Hi, @StefanoMilanesi-8258

Use Newtonsoft’s Json.NET instead of System.Text.Json. System.Text.Json is more strict than NewtonsoftJson.

How to replace?

Step 1. Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

78159-image.png

Step 2. services.AddRazorPages().AddNewtonsoftJson();

Adding the AddNewtonsoftJson() call to the end means that we’re going to use the Newtonsoft.Json APIs over the default System.Text.Json implementation.

Then, change the codes of controller like below

         [HttpGet]
         [ActionName("GetTimesheets")]
         [AllowAnonymous]
         public async Task<IActionResult> GetTimesheets([FromBody] TimeSheetBO timeSheetBO)
         {
             (List<ListTimesheets> ListTimesheetsTmp, Errors ErrorTmp) = TimesheetDAL.GetUserTimesheets(timeSheetBO);
             return Json((ListTimesheetsTmp, ErrorTmp));
         }

Test result

78166-image.png


If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Best Regards,
Michael Wang


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

StefanoMilanesi-8258 avatar image
0 Votes"
StefanoMilanesi-8258 answered

Hi,
thank you for the reply.
But I can't install the package recommended (Microsoft.AspNetCore.Mvc.NewtonsoftJson) because it need the .Net Core version 5.0 and my solution has .NET Core 2.1.
The minimum version accepted by the older version of "Microsoft.AspNetCore.Mvc.NewtonsoftJso" is .Net Core 3.0.

Thanks
Stefano

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.