Darkseal / ASP.NET-Core-Web-API

Code repository for the Building Web APIs with ASP.NET Core Manning book by Valerio De Sanctis
https://bit.ly/4zQsrZn
MIT License
59 stars 20 forks source link

Chapter 2 - Board Games JSON Format Question #4

Closed drdexter33 closed 10 months ago

drdexter33 commented 10 months ago

At the end of Chapter 2 the book is showing the API result from the https://localhost:55221/boardgames endpoint as being formatted:

image

My results are not formatted in my web browser:

image

Should they be?

If I run this in Postman and select Pretty Print, it's formatting correctly:

image

thanks.

Darkseal commented 10 months ago

Hello there,

first of all: thanks a lot for purchasing my book :)

Regarding your question, the JSON formatting/indentation depends on the browser you use (some apply indentation & pretty print by default, some don't) and/or how you have configured the Json default settings in your Program.cs file.

For example, this is how you can activate indentation for all JSON outputs:

builder.Services.AddControllers()
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.WriteIndented = true;
            });

For additional info, check out this page.

drdexter33 commented 10 months ago

Perfect!

image

thanks!