-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Issue with multipart/form-data post and json.net deserializtion (using interfaces) #4868
Copy link
Copy link
Open
Labels
Needs: DesignThis issue requires design work before implementating.This issue requires design work before implementating.affected-mediumThis issue impacts approximately half of our customersThis issue impacts approximately half of our customersarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesenhancementThis issue represents an ask for new feature or an enhancement to an existing oneThis issue represents an ask for new feature or an enhancement to an existing onefeature-model-bindingseverity-blockingThis label is used by an internal toolThis label is used by an internal tool
Milestone
Description
We use the aspnetcore 2.
We have configured JsonOptions in startup like this.
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.Converters = new List<JsonConverter> {
new JsonDocumentConverter(),
new JsonDocumentMetaConverter(),
//new Bll.FileRecords.JsonDocumentMetaListConverter(),
};
});
public class JsonDocumentConverter : CustomCreationConverter<IDocument>
{
public override IDocument Create(Type objectType)
{
return new LocalDocument();
}
}
public class JsonDocumentMetaConverter : CustomCreationConverter<IDocumentMeta>
{
public override IDocumentMeta Create(Type objectType)
{
return new LocalDocumentMeta();
}
}In our controller we have 2 methods
- The first one is called PostUrlencoded in which we make a http post request with content-type : application/x-www-form-urlencoded.
[HttpPost("PostUrlencoded")]
public async Task<IActionResult> 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.
[HttpPost("PostΜultipart")]
public async Task<IActionResult> 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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Needs: DesignThis issue requires design work before implementating.This issue requires design work before implementating.affected-mediumThis issue impacts approximately half of our customersThis issue impacts approximately half of our customersarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesenhancementThis issue represents an ask for new feature or an enhancement to an existing oneThis issue represents an ask for new feature or an enhancement to an existing onefeature-model-bindingseverity-blockingThis label is used by an internal toolThis label is used by an internal tool