From b2cb05c32aca8b4c9746885d178b4d2e9b75b500 Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Wed, 14 Sep 2022 16:26:23 -0700 Subject: [PATCH] Update JSON options examples (#26996) --- aspnetcore/tutorials/min-web-api.md | 14 +++--- .../samples/7.x/WebMinJson/Program.cs | 47 +++++++++++++++++++ .../samples/7.x/WebMinJson/WebMinJson.csproj | 9 ++++ .../WebMinJson/appsettings.Development.json | 9 ++++ .../samples/7.x/WebMinJson/appsettings.json | 10 ++++ .../min-web-api/samples/7.x/stub.txt | 1 - 6 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs create mode 100644 aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/WebMinJson.csproj create mode 100644 aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/appsettings.Development.json create mode 100644 aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/appsettings.json delete mode 100644 aspnetcore/tutorials/min-web-api/samples/7.x/stub.txt diff --git a/aspnetcore/tutorials/min-web-api.md b/aspnetcore/tutorials/min-web-api.md index 78f36e60de..dbceee08f5 100644 --- a/aspnetcore/tutorials/min-web-api.md +++ b/aspnetcore/tutorials/min-web-api.md @@ -927,17 +927,19 @@ Verify you can't post or get the secret field. - No support for [JsonPatch](https://www.nuget.org/packages/Microsoft.AspNetCore.JsonPatch/) - No support for [OData](https://www.nuget.org/packages/Microsoft.AspNetCore.OData/) - No support for [ApiVersioning](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Versioning/). See [this issue](https://github.com/dotnet/aspnet-api-versioning/issues/751) for more details. -## Use JsonOptions -The following code uses : +## Configure JSON serialization options -[!code-csharp[](min-web-api/samples/6.x/WebMinJson/Program.cs?name=snippet_1)] +The following example invokes [`ConfigureHttpJsonOptions`](https://source.dot.net/#Microsoft.AspNetCore.Http.Extensions/HttpJsonServiceExtensions.cs,496f2a8225e6c731) to configure options that apply wherever the app serializes or deserializes JSON for HTTP requests and responses: -The following code uses : +[!code-csharp[](min-web-api/samples/7.x/WebMinJson/Program.cs?name=snippet_1)] -[!code-csharp[](min-web-api/samples/6.x/WebMinJson/Program.cs?name=snippet_2)] +Options that you configure by invoking `ConfigureHttpJsonOptions` apply when the app calls extension methods defined in or . + +To make more localized changes to the serialization options, pass modified versions of directly into the responses that are being sent from endpoints, as shown in the following example: + +[!code-csharp[](min-web-api/samples/7.x/WebMinJson/Program.cs?name=snippet_2)] -The preceding code uses [web defaults](/dotnet/standard/serialization/system-text-json-configure-options#web-defaults-for-jsonserializeroptions), which converts property names to camel case. ## Test minimal API diff --git a/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs new file mode 100644 index 0000000000..a5ca94c89c --- /dev/null +++ b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs @@ -0,0 +1,47 @@ +#define First +#if First +// +var builder = WebApplication.CreateBuilder(args); + +builder.Services.ConfigureHttpJsonOptions(options => +{ + options.SerializerOptions.IncludeFields = true; +}); + +var app = builder.Build(); + +app.MapGet("/", () => new Todo { Name = "Walk dog", IsComplete = false }); + +app.Run(); + +class Todo +{ + public string? Name; + public bool IsComplete; +} +// +#else +// +using System.Text.Json; + +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +var options = new JsonSerializerOptions +{ + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true +} + +app.MapGet("/", () => Results.Json(new Todo { + Name = "Walk dog", IsComplete = false }, options)); + +app.Run(); + +class Todo +{ + public string? Name { get; set; } + public bool IsComplete { get; set; } +} +// +#endif diff --git a/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/WebMinJson.csproj b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/WebMinJson.csproj new file mode 100644 index 0000000000..9f5e622c5c --- /dev/null +++ b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/WebMinJson.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/appsettings.Development.json b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/appsettings.Development.json new file mode 100644 index 0000000000..8983e0fc1c --- /dev/null +++ b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/appsettings.json b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/appsettings.json new file mode 100644 index 0000000000..d9d9a9bff6 --- /dev/null +++ b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/aspnetcore/tutorials/min-web-api/samples/7.x/stub.txt b/aspnetcore/tutorials/min-web-api/samples/7.x/stub.txt deleted file mode 100644 index 0b89473fc7..0000000000 --- a/aspnetcore/tutorials/min-web-api/samples/7.x/stub.txt +++ /dev/null @@ -1 +0,0 @@ -Delete after adding 7.x version.