Skip to content

Commit 9dc00e9

Browse files
authored
Address feedback in issue #35629 (#35781)
1 parent 82fc086 commit 9dc00e9

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

aspnetcore/mvc/models/model-binding.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ author: tdykstra
44
description: Learn how model binding in ASP.NET Core works and how to customize its behavior.
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: tdykstra
7-
ms.date: 6/20/2023
7+
ms.date: 07/19/2025
8+
ai-usage: ai-assisted
89
uid: mvc/models/model-binding
910
---
1011

@@ -631,7 +632,17 @@ Used to retrieve all the values from posted form data.
631632

632633
## Input formatters
633634

634-
Data in the request body can be in JSON, XML, or some other format. To parse this data, model binding uses an *input formatter* that is configured to handle a particular content type. By default, ASP.NET Core includes JSON based input formatters for handling JSON data. You can add other formatters for other content types.
635+
Data in the request body can be in JSON, XML, or some other format. To parse this data, model binding uses an *input formatter* that is configured to handle a particular content type. By default, ASP.NET Core includes JSON based input formatters for handling JSON data using [`System.Text.Json`](/dotnet/standard/serialization/system-text-json-overview). You can add other formatters for other content types.
636+
637+
The default JSON input formatter can be configured using the `AddJsonOptions` method:
638+
639+
:::code language="csharp" source="~/mvc/models/model-binding/samples/6.x/ModelBindingSample/Snippets/Program.cs" id="snippet_AddJsonOptions":::
640+
641+
Common configuration options include:
642+
643+
* **Property naming policy** - Configure camelCase or other naming conventions
644+
* **Enum converters** - Handle enum serialization as strings
645+
* **Custom converters** - Add type-specific serialization logic
635646

636647
ASP.NET Core selects input formatters based on the [Consumes](xref:Microsoft.AspNetCore.Mvc.ConsumesAttribute) attribute. If no attribute is present, it uses the [Content-Type header](https://www.w3.org/Protocols/rfc1341/4_Content-Type.html).
637648

aspnetcore/mvc/models/model-binding/samples/6.x/ModelBindingSample/Snippets/Program.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Microsoft.AspNetCore.Mvc.ModelBinding;
22
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
35

46
namespace ModelBindingSample.Snippets;
57

@@ -51,4 +53,22 @@ public static void ExcludeSuppressModelBinding(WebApplicationBuilder builder)
5153
});
5254
// </snippet_ModelMetadataDetailsProviders>
5355
}
56+
57+
public static void AddJsonOptions(WebApplicationBuilder builder)
58+
{
59+
// <snippet_AddJsonOptions>
60+
builder.Services.AddControllers().AddJsonOptions(options =>
61+
{
62+
// Configure property naming policy (camelCase)
63+
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
64+
65+
// Add enum converter to serialize enums as strings
66+
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
67+
68+
// Configure other JSON options
69+
options.JsonSerializerOptions.WriteIndented = true;
70+
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
71+
});
72+
// </snippet_AddJsonOptions>
73+
}
5474
}

0 commit comments

Comments
 (0)