I have a custom model binder, being used in a REST API, which looks as follows:
public class CustomQueryModelBinder : IModelBinder
{
public Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext)
{
if (!String.IsNullOrWhiteSpace(bindingContext.ModelName) && bindingContext.ModelType == typeof(short) && bindingContext.ValueProvider.GetValue(bindingContext.ModelName) != null)
{
short value;
var val = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).FirstValue as string;
if (String.IsNullOrWhiteSpace(val))
{
return ModelBindingResult.SuccessAsync(bindingContext.ModelName, val);
}
else if (Int16.TryParse(val, out value) && value >= 0)
{
return ModelBindingResult.SuccessAsync(bindingContext.ModelName, value);
}
else
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "The value is invalid.");
}
}
return ModelBindingResult.FailedAsync(bindingContext.ModelName);
}
}
And in cases where the custom value is not specified in the URI it should default to a valid value (greater than 0) however it is always defaulting to 0, even though the controller looks as follows:
public async Task<IActionResult> GetAsync(
[ModelBinder(BinderType = typeof(CustomQueryModelBinder))]short value = 100,
Basically value here should be getting set to 100 as its default value when it returns as null from the ModelBinder.
However this is not happening and it is constantly being returned as 0 which is resulting in System.ArgumentOutOfRangeException when trying to do a Get.
We are using RC1.