dotnet / dotnet/aspnetcore

Give us an option to enable loose binding for invalid data and providing default values for invalid properties in JSON in HTTP Post

Open
#56,663 0 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

Consider this JSON (which of course is invalid):

```
{
"name": "Somebody",
"age": "24",
"married": ""
}
```

And this action:

```
[HttpPost]
public IActionResult DoSomething([FromBody] Person person)
{
// person is null here
}
```

Let us configure a simple boolean setting in our ASP.NET Core pipeline to enable invalid-data binding as much as it can. For example in this model, the invalid "24" can be converted to numeric values and be bound as a valid integer. The invalid empty string for the boolean attribute can be bound as the default false value.

### Describe the solution you'd like

I would like to be able to configure a setting like:

```
EnableInvalidDataModelBinding = true
```

### Additional context

Right now we should create custom model binders and that's a tedious task and more importantly error-prone. It might even cause security issues. It's better that we rely on something out of the box.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.