Update JSON options examples (#26996)
parent
52c4ade6fb
commit
b2cb05c32a
|
@ -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 <xref:Microsoft.AspNetCore.Http.Json.JsonOptions>:
|
||||
## 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 <xref:System.Text.Json.JsonSerializerOptions>:
|
||||
[!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 <xref:Microsoft.AspNetCore.Http.HttpResponseJsonExtensions> or <xref:Microsoft.AspNetCore.Http.HttpRequestJsonExtensions>.
|
||||
|
||||
To make more localized changes to the serialization options, pass modified versions of <xref:System.Text.Json.JsonSerializerOptions> 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
|
||||
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
#define First
|
||||
#if First
|
||||
// <snippet_1>
|
||||
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;
|
||||
}
|
||||
// </snippet_1>
|
||||
#else
|
||||
// <snippet_2>
|
||||
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; }
|
||||
}
|
||||
// </snippet_2>
|
||||
#endif
|
|
@ -0,0 +1,9 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
Delete after adding 7.x version.
|
Loading…
Reference in New Issue