Code · 175 lines · 3497 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175# VanDerHeijden.JsonBodyProvider

**ASP.NET Core custom value provider**  
Allows you to **bind multiple simple parameters directly from a JSON request body** — without using `[FromBody]` and without wrapper DTO classes.

**Main purpose**  
Call controller actions like this:

```csharp
[HttpPost]
public IActionResult CreateUser(
    string Name,
    int Age,
    List<string> Roles,
    Guid TenantId,
    DateOnly StartDate,
    bool IsActive,
    string? Comment = null)
{
    // ← all parameters automatically come from JSON body
}
```

## Most important facts first

**Works only without** `[ApiController]` attribute  
`[ApiController]` breaks this completely → **remove it** from the controller

## Installation

```bash
dotnet add package JsonBodyProvider
```

## Recommended minimal startup configuration

```csharp
builder.Services
    .AddControllers()                       // ← no [ApiController] !
    .AddJsonBodyProvider(CorrectLists: true);  // ← enables normal list + string behaviour
```

## Good real-world examples

### Example 1 – Classic user creation

```csharp
[Route("api/users")]
public class UsersController : ControllerBase
{
    [HttpPost]
    public IActionResult Create(
        string Name,
        int Age,
        string Email,
        List<string> Roles,
        bool IsActive = true)
    {
        // ...
    }
}
```

```json
POST /api/users
Content-Type: application/json

{
  "Name": "Lisa van Dijk",
  "Age": 34,
  "Email": "lisa@example.com",
  "Roles": ["editor", "reviewer"],
  "IsActive": true
}
```

### Example 2 – Order with items

```csharp
[HttpPost("orders")]
public IActionResult PlaceOrder(
    string OrderNumber,
    DateOnly OrderDate,
    decimal TotalAmount,
    List<string> ProductCodes,
    Guid CustomerId,
    string? Remark = null)
{
    // ...
}
```

```json
{
  "OrderNumber": "ORD-2025-78412",
  "OrderDate": "2025-06-18",
  "TotalAmount": 1249.95,
  "ProductCodes": ["PROD-A42", "PROD-B17", "PROD-C09"],
  "CustomerId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
  "Remark": ""
}
```

### Example 3 – Very flat analytics event

```csharp
[HttpPost("events")]
public IActionResult TrackEvent(
    string EventName,
    string UserId,
    DateTime Timestamp,
    int Value,
    Dictionary<string, string> Properties,
    bool IsConversion,
    Guid? CampaignId)
{
    // ...
}
```

```json
{
  "EventName": "purchase_completed",
  "UserId": "usr_9k3j2p8x",
  "Timestamp": "2025-06-18T14:22:09Z",
  "Value": 1,
  "Properties": {
    "Category": "electronics",
    "Source": "newsletter"
  },
  "IsConversion": true,
  "CampaignId": null
}
```

### Example 4 – Very minimal update

```csharp
[HttpPatch("items/{id}")]
public IActionResult PatchItem(
    Guid id,
    string? Title,
    string? Description,
    bool? Active,
    int? Priority)
{
    // only the fields that are sent will be bound
}
```

```json
{
  "Title": "Updated task name",
  "Priority": 3
}
```

## What you get for free

- Multiple parameters from **one** JSON body
- No need for `[FromBody]` on every parameter
- No wrapper DTO needed
- Empty strings stay empty strings
- Lists behave nicely (no random fallback to default(T) on parse errors)
- Headers and cookies are also available as parameters (optional)

## Golden rule (repeat until it hurts)

**Do NOT use [ApiController] on controllers where you want to use this package**

If you really need `[ApiController]` → you cannot use this style of multi-parameter binding

## License

MIT