question

StefanoMilanesi-1623 avatar image
0 Votes"
StefanoMilanesi-1623 asked StefanoMilanesi-1623 commented

Blazor server: return more objects from endpoint

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

dotnet-aspnet-core-blazor
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.

BruceBarker-8516 avatar image
0 Votes"
BruceBarker-8516 answered StefanoMilanesi-1623 commented

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

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

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

BruceBarker-8516 avatar image
0 Votes"
BruceBarker-8516 answered

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});

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-1623 avatar image
0 Votes"
StefanoMilanesi-1623 answered

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

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.