Blazor server: return more objects from endpoint

Stefano Milanesi 21 Reputation points
2021-07-07T16:57:45.84+00:00

Hi,
I'm developing a Blazor Server application and I "declared" this EndPoint in the "Configure" of the Startup.cs file:

            endpoints.MapGet("/products/GetProduct/{ProductType}/{ProductSerialNumber}", (context) =>
            {
                string ProductType = Convert.ToString(context.GetRouteValue("ProductType"));
                string ProductSerialNumber = Convert.ToString(context.GetRouteValue("ProductSerialNumber"));
                (var product , var errors) = app.ApplicationServices.GetService<ProductsServices>().GetProduct(ProductType, ProductSerialNumber);
                var json = JsonSerializer.Serialize<Product>(product);
                return context.Response.WriteAsync(json);
            });

How can I get the "errors" object back too? is it possible to manage the return of two or more objects?

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,557 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce Barker 801 Reputation points
    2021-07-08T21:37:37.377+00:00

    the sample is a json object, a json array would be:

    var json = JsonSerializer.Serialize(new object[] {errors, product});


2 additional answers

Sort by: Most helpful
  1. Bruce Barker 801 Reputation points
    2021-07-07T22:09:41.257+00:00

    just depends on what you want the response json to look like. do you want an array, or object, try:

    var json = JsonSerializer.Serialize(new {errors = errors, product = product});

    0 comments No comments

  2. Stefano Milanesi 21 Reputation points
    2021-07-08T08:35:13.867+00:00

    Hi,
    thanks for the reply. Your solution works (an and array)
    it would be better as an object, in this case how is the syntax?

    Best
    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.