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

Stefano Milanesi 51 Reputation points
2021-03-15T11:04:15.067+00:00

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

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,595 questions
{count} votes

Accepted answer
  1. Michael Wang-MSFT 1,061 Reputation points
    2021-03-17T10:02:08.057+00:00
    <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

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Stefano Milanesi 51 Reputation points
    2021-03-16T07:55:10.637+00:00

    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

    0 comments No comments

  2. Michael Wang-MSFT 1,061 Reputation points
    2021-03-16T10:24:47.073+00:00

    Hi, @Stefano Milanesi

    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

    0 comments No comments

  3. Michael Wang-MSFT 1,061 Reputation points
    2021-03-16T10:24:47.343+00:00

    Hi, @Stefano Milanesi

    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

    0 comments No comments

  4. Stefano Milanesi 51 Reputation points
    2021-03-16T14:42:43.99+00:00

    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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.