Custom ModelBinder not fired when used with FromBody on a sub property #4553
Comments
public class RegisteredCreationModel
{
[Required]
[FromBody]
public string Name { get; set; }
[ModelBinder(BinderType = typeof(ExtendedValuesModelBinder))]
public Dictionary<string, string> ExtendedValues { get; set; }
} |
Thx for your reply. |
Can't see what's going on without more information. Please upload a small repro project (preferably a GitHub repo) and provide a link here. |
I just create a repo here : https://github.com/sebastieno/aspnetcore-issue4553 You can post data The call to the first url does not trigger my model binder. The call to the second url trigger the model binder but with empty data Tell me if you need more informations. Thx. |
I wasn't clear, sorry. The original reported issue and the sample repo are working as expected: Model binding isn't involved once the formatter takes over. So My request for more information was about
|
I add an action to the sample : https://github.com/sebastieno/aspnetcore-issue4553 You can post data |
|
Indeed, Do you consider this behavior as an issue ? Do you think that this will be fixed in the RC2 ? Thx for your time and your help. |
@sebastieno I am having trouble understanding your last response. You said both that you want to post JSON and that you're using a form-URL-encoded body. The two can't both be true. More generally what should be "fixed" here? The system has a few relevant features but nothing that's behaving unexpectedly in your scenario:
One option may be to write a custom value provider factory that creates a value provider for a JSON request body -- turning that body into name / value pairs. Don't use |
Ok. I will try to explain better. My need is to post JSON data to an action. In that data, I have static fields, like the Actually I post data as form-URL-encoded, but it's a workaround. I can use another example. Imagine I have a decimal property. How can I do to be able to bind values |
The scenario you're describing wouldn't work in ASP.NET 4 unless the body was form-URL-encoded. The problem here is attempting to mix in JSON and input formatters. Stick w/ a form-URL-encoded body and add a custom value provider or model binder where needed. |
I take the time to create my own JsonInputFormatter. I just duplicate the classic JsonInputFormatter and change how the deserialization is done https://github.com/aspnet/mvc/blob/master/src/Microsoft.AspNet.Mvc.Formatters.Json/JsonInputFormatter.cs#L117. Here my implementation : try
{
var jsonModel = await jsonReader.ReadToEndAsync();
model = jsonSerializer.Deserialize(new StringReader(jsonModel), type);
var extendedValuesModel = model as IExtendedValuesModel;
if (extendedValuesModel != null)
{ Do you think is the right place ? |
Since |
Ok. Thanks a lot for your time and your help :) |
I have a model that looks like this :
In my controller, I add the
FromBody
attribut to bind the parameter model from the body :The problem I have is that my custom model binder is not called. If I remove the
FromBody
attribut, my model binder is called, but each property is empty (bindingContext.ValueProvider
). If I move theModelBinder(BinderType = typeof(ExtendedValuesModelBinder))
attribut to theRegisteredCreationModel
class, and leave theFromBody
attribut on my action, properties are defined and my model binder is called.In my case, I really need to set the custom model binder on a property of my model. Did I do something wrong ? If it's an issue, what's the workaround ? Thanks for your help.
The text was updated successfully, but these errors were encountered: