dotnet / dotnet/aspnetcore

Issue with multipart/form-data post and json.net deserializtion (using interfaces)

Open
#4,868 38 comments 24 reactions 0 assignees View on GitHub
affected-medium area-mvc enhancement feature-model-binding Needs: Design severity-blocking
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

We use the aspnetcore 2.

We have configured JsonOptions in startup like this.
```csharp
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.Converters = new List {
new JsonDocumentConverter(),
new JsonDocumentMetaConverter(),
//new Bll.FileRecords.JsonDocumentMetaListConverter(),
};
});

public class JsonDocumentConverter : CustomCreationConverter
{
public override IDocument Create(Type objectType)
{
return new LocalDocument();
}
}
public class JsonDocumentMetaConverter : CustomCreationConverter
{
public override IDocumentMeta Create(Type objectType)
{
return new LocalDocumentMeta();
}
}
```

In our controller we have 2 methods
1. The first one is called PostUrlencoded in which we make a http post request with content-type : application/x-www-form-urlencoded.
```csharp
[HttpPost("PostUrlencoded")]
public async Task PostUrlencoded([FromBody]IDocument document)
{ /*...*/ }
```
2.. The second one is called PostΜultipart in which we make a http post request with content-type : multipart/form-data.
```csharp
[HttpPost("PostΜultipart")]
public async Task PostΜultipart([FromForm]IDocument document)
{ /*...*/ }
```

The first method works fine, and the deserialization works fine. In the second method deserialization fails.
The error is:
`Model bound complex types must not be abstract or value types and must have a parameterless constructor.`

That's because the JsonOptions isn't applied.

When we remove the jsonOptions from the startup.cs, we get the same error in both cases.

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.