Open phillip-haydon opened 10 years ago
DecimalConverter.ConvertFrom is used in the context of current culture, It expects "1,1" instead of "1.1" in Turkish culture. A fallback strategy can be implemented by using ConvertFrom with InvariantCulture. Do you accept pull requests ?
Just write own ITypeConverter
/// <summary>
/// Nancy converter to convert numeric types with InvariantCulture.
/// </summary>
public class NancyNumericConverter : ITypeConverter
{
public bool CanConvertTo(Type destinationType, BindingContext context)
{
return destinationType.IsNumeric();
}
public object Convert(string input, Type destinationType, BindingContext context)
{
if (string.IsNullOrEmpty(input))
{
return null;
}
return System.Convert.ChangeType(input, destinationType, CultureInfo.InvariantCulture);
}
}
Just write own
ITypeConverter
Thanks for the suggestion, it works great!
But looks like System.Convert.ChangeType
doesn't support nullable types.
Can be fixed using Nullable.GetUnderlyingType
like this:
/// <summary>
/// Nancy converter to convert numeric types with InvariantCulture.
/// </summary>
public class InvariantCultureNumericConverter : ITypeConverter
{
public bool CanConvertTo(Type destinationType, BindingContext context)
{
return destinationType.IsNumeric();
}
public object Convert(string input, Type destinationType, BindingContext context)
{
if (string.IsNullOrEmpty(input))
{
return null;
}
destinationType = Nullable.GetUnderlyingType(destinationType) ?? destinationType;
return System.Convert.ChangeType(input, destinationType, CultureInfo.InvariantCulture);
}
}
http://stackoverflow.com/questions/24439997/nancy-decimal-property-binding-not-working
Looks like this is bug, tried it out myself with a route and decimal and got same exception.