From 83433f9b61f5590c6de9d39fb606037684375e5c Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Wed, 14 Sep 2022 11:15:54 -0700 Subject: [PATCH 1/9] Update JSON options examples --- aspnetcore/tutorials/min-web-api.md | 13 +++-- .../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(+), 6 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 78f36e60deda..73969ba837a9 100644 --- a/aspnetcore/tutorials/min-web-api.md +++ b/aspnetcore/tutorials/min-web-api.md @@ -927,15 +927,18 @@ 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` 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 specify by using `ConfigureHttpJsonOptions` will apply whenever your app calls extension methods defined in and . + +To make more localized changes to the serialization options, you can pass modified versions of directly into the responses that are being sent from your endpoint, 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. 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 000000000000..3fac5f7c2b88 --- /dev/null +++ b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs @@ -0,0 +1,47 @@ +#define Second +#if First +#region snippet_1 +using Microsoft.AspNetCore.Http.Json; + +var builder = WebApplication.CreateBuilder(args); + +// Configure JSON options +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 +{ + // These are public fields instead of properties. + public string? Name; + public bool IsComplete; +} +#endregion +#else +#region snippet_2 +using System.Text.Json; + +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +var options = new JsonSerializerOptions(JsonSerializerDefaults.Web); + +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; } +} +#endregion +#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 000000000000..9f5e622c5cf3 --- /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 000000000000..8983e0fc1c5e --- /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 000000000000..d9d9a9bff6fd --- /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 0b89473fc7cb..000000000000 --- a/aspnetcore/tutorials/min-web-api/samples/7.x/stub.txt +++ /dev/null @@ -1 +0,0 @@ -Delete after adding 7.x version. From c2b644dc177c753a719a1bb39a1a5cdd581a1930 Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Wed, 14 Sep 2022 13:03:59 -0700 Subject: [PATCH 2/9] Update aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs Co-authored-by: Safia Abdalla --- .../tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 index 3fac5f7c2b88..4c79dde70351 100644 --- a/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs +++ b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs @@ -31,7 +31,11 @@ class Todo var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); -var options = new JsonSerializerOptions(JsonSerializerDefaults.Web); +var options = new JsonSerializerOptions +{ + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true +} app.MapGet("/", () => Results.Json(new Todo { Name = "Walk dog", IsComplete = false }, options)); From 10ca57667f1c5322896cafed388fab01e538eed7 Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Wed, 14 Sep 2022 13:05:44 -0700 Subject: [PATCH 3/9] Update aspnetcore/tutorials/min-web-api.md --- aspnetcore/tutorials/min-web-api.md | 1 - 1 file changed, 1 deletion(-) diff --git a/aspnetcore/tutorials/min-web-api.md b/aspnetcore/tutorials/min-web-api.md index 73969ba837a9..42d15be27fe3 100644 --- a/aspnetcore/tutorials/min-web-api.md +++ b/aspnetcore/tutorials/min-web-api.md @@ -940,7 +940,6 @@ To make more localized changes to the serialization options, you can pass modifi [!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 From 9034ce76244a778254a21588018cbf4401d6e37b Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Wed, 14 Sep 2022 14:40:41 -0700 Subject: [PATCH 4/9] snippet tags --- .../min-web-api/samples/7.x/WebMinJson/Program.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) 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 index 4c79dde70351..de08b1012b91 100644 --- a/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs +++ b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs @@ -1,8 +1,6 @@ -#define Second +#define First #if First -#region snippet_1 -using Microsoft.AspNetCore.Http.Json; - +// var builder = WebApplication.CreateBuilder(args); // Configure JSON options @@ -23,9 +21,9 @@ class Todo public string? Name; public bool IsComplete; } -#endregion +// #else -#region snippet_2 +// using System.Text.Json; var builder = WebApplication.CreateBuilder(args); @@ -47,5 +45,5 @@ class Todo public string? Name { get; set; } public bool IsComplete { get; set; } } -#endregion +// #endif From 147ec6a33626df4998f229241c1efd285849dc99 Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Wed, 14 Sep 2022 16:01:16 -0700 Subject: [PATCH 5/9] Update aspnetcore/tutorials/min-web-api.md Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- aspnetcore/tutorials/min-web-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/tutorials/min-web-api.md b/aspnetcore/tutorials/min-web-api.md index 42d15be27fe3..6482f4e3e393 100644 --- a/aspnetcore/tutorials/min-web-api.md +++ b/aspnetcore/tutorials/min-web-api.md @@ -936,7 +936,7 @@ The following example invokes `ConfigureHttpJsonOptions` to configure options th Options that you specify by using `ConfigureHttpJsonOptions` will apply whenever your app calls extension methods defined in and . -To make more localized changes to the serialization options, you can pass modified versions of directly into the responses that are being sent from your endpoint, as shown in the following example: +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)] From 70d51dc51872205841d6eae20a0cd56e9120f04a Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Wed, 14 Sep 2022 16:03:05 -0700 Subject: [PATCH 6/9] Update aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- .../tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs | 1 - 1 file changed, 1 deletion(-) 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 index de08b1012b91..b6f85dc82228 100644 --- a/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs +++ b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs @@ -17,7 +17,6 @@ class Todo { - // These are public fields instead of properties. public string? Name; public bool IsComplete; } From e61f7be9999933005556c014891cc64c7824e1b8 Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Wed, 14 Sep 2022 16:03:51 -0700 Subject: [PATCH 7/9] Update aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- .../tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs | 1 - 1 file changed, 1 deletion(-) 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 index b6f85dc82228..a5ca94c89c2d 100644 --- a/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs +++ b/aspnetcore/tutorials/min-web-api/samples/7.x/WebMinJson/Program.cs @@ -3,7 +3,6 @@ // var builder = WebApplication.CreateBuilder(args); -// Configure JSON options builder.Services.ConfigureHttpJsonOptions(options => { options.SerializerOptions.IncludeFields = true; From cf5e8eed8c6ae30122838104ec93418f3fb1f4d2 Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Wed, 14 Sep 2022 16:08:15 -0700 Subject: [PATCH 8/9] Update aspnetcore/tutorials/min-web-api.md Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- aspnetcore/tutorials/min-web-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/tutorials/min-web-api.md b/aspnetcore/tutorials/min-web-api.md index 6482f4e3e393..fc182ac4288d 100644 --- a/aspnetcore/tutorials/min-web-api.md +++ b/aspnetcore/tutorials/min-web-api.md @@ -934,7 +934,7 @@ The following example invokes `ConfigureHttpJsonOptions` to configure options th [!code-csharp[](min-web-api/samples/7.x/WebMinJson/Program.cs?name=snippet_1)] -Options that you specify by using `ConfigureHttpJsonOptions` will apply whenever your app calls extension methods defined in and . +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: From be7a5c04b574ce5973464a0ef0c1b401f91b6404 Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Wed, 14 Sep 2022 16:09:13 -0700 Subject: [PATCH 9/9] Update aspnetcore/tutorials/min-web-api.md Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- aspnetcore/tutorials/min-web-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/tutorials/min-web-api.md b/aspnetcore/tutorials/min-web-api.md index fc182ac4288d..dbceee08f529 100644 --- a/aspnetcore/tutorials/min-web-api.md +++ b/aspnetcore/tutorials/min-web-api.md @@ -930,7 +930,7 @@ Verify you can't post or get the secret field. ## Configure JSON serialization options -The following example invokes `ConfigureHttpJsonOptions` to configure options that apply wherever the app serializes or deserializes JSON for HTTP requests and responses: +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: [!code-csharp[](min-web-api/samples/7.x/WebMinJson/Program.cs?name=snippet_1)]