Для этого вам нужно создать пользовательский механизм связывания моделей.
1.Создать StringToBoolBinder
public class StringToBoolBinder: ComplexTypeModelBinder
{
IDictionary<ModelMetadata, IModelBinder> _propertyBinders;
public StringToBoolBinder(IDictionary<ModelMetadata, IModelBinder> propertyBinders) : base(propertyBinders)
{
_propertyBinders = propertyBinders;
}
protected override Task BindProperty(ModelBindingContext bindingContext)
{
if (bindingContext == null)
throw new ArgumentNullException(nameof(bindingContext));
string valueFromBody = string.Empty;
using (var sr = new StreamReader(bindingContext.HttpContext.Request.Body))
{
valueFromBody = sr.ReadToEnd();
}
if (string.IsNullOrEmpty(valueFromBody))
{
return Task.CompletedTask;
}
if (bindingContext.FieldName == "Success")
{
var json = JObject.Parse(valueFromBody);
string values = Convert.ToString(((JValue)JObject.Parse(valueFromBody)["prefix_success"]).Value);
if (values == "1")
{
bindingContext.Result = ModelBindingResult.Success(true);
}
else
{
bindingContext.Result = ModelBindingResult.Success(false);
}
}else
{
return base.BindProperty(bindingContext);
}
return Task.CompletedTask;
}
}
2.Создать StringToBoolBinderProvider
public class StringToBoolBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (context.Metadata.IsComplexType && context.Metadata.ModelType == typeof(RequestForm))
{
var propertyBinders = new Dictionary<ModelMetadata, IModelBinder>();
for (var i = 0; i < context.Metadata.Properties.Count; i++)
{
var property = context.Metadata.Properties[i];
propertyBinders.Add(property, context.CreateBinder(property));
}
return new StringToBoolBinder(propertyBinders);
}
return null;
}
}
3. Зарегистрировать провайдера при запуске
services.AddMvc(options =>
{
// add custom binder to beginning of collection
options.ModelBinderProviders.Insert(0, new StringToBoolBinderProvider());
});
4.Action
[HttpPost]
public void Post([FromBody] RequestForm form)
5.Model
public class RequestForm {
...
public bool Success { get; set; }
...
}
6.Json Полезная нагрузка
{
...
"prefix_success":"1",
...
}