http://stackoverflow.com/questions/24439997/nancy-decimal-property-binding-not-working
Oh noes! ---> Nancy.ModelBinding.ModelBindingException: Unable to bind to type: Nancy.Models.SomeModel
at Nancy.ModelBinding.DefaultBinder.Bind(NancyContext context, Type modelType, Object instance, BindingConfig configuration, String[] blackList)
at Nancy.ModelBinding.DynamicModelBinderAdapter.TryConvert(ConvertBinder binder, Object& result)
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at Nancy.ModelBinding.ModuleExtensions.Bind[TModel](INancyModule module)
at KBZServisNancy.Modules.SomeModule.<.ctor>b__2(Object x) in d:\Nancy\Modules\SomeDecimalModule.cs:line 38
at CallSite.Target(Closure , CallSite , Func`2 , Object )
at Nancy.Routing.Route.<>c__DisplayClass4.b__3(Object parameters, CancellationToken context)
--- End of inner exception stack trace ---
at Nancy.NancyEngine.InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exception ex)
at Nancy.NancyEngine.InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exception ex)
これはバグのようです。ルートと小数を使って自分で試してみましたが、同じ例外が発生しました。
DecimalConverter.ConvertFromは、現在のカルチャのコンテキストで使用されます。トルコのカルチャでは、「1.1」ではなく「1,1」を想定しています。 フォールバック戦略は、ConvertFromとInvariantCultureを使用して実装できます。 プルリクエストを受け入れますか?
自分のITypeConverter
書くだけです
`` `c#
///
/// InvariantCultureを使用して数値型を変換するナンシーコンバーター。
///
パブリッククラスNancyNumericConverter:ITypeConverter
{{
public bool CanConvertTo(Type destinationType、BindingContext context)
{{
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);
}
}
`` `
自分の
ITypeConverter
書くだけです
提案をありがとう、それは素晴らしい働きをします!
しかし、 System.Convert.ChangeType
はnull許容型をサポートしていないようです。
次のようにNullable.GetUnderlyingType
を使用して修正できます。
/// <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);
}
}
最も参考になるコメント
自分の
ITypeConverter
書くだけです`` `c#
///
/// InvariantCultureを使用して数値型を変換するナンシーコンバーター。
///
パブリッククラスNancyNumericConverter:ITypeConverter
{{
public bool CanConvertTo(Type destinationType、BindingContext context)
{{
destinationType.IsNumeric();を返します。
}
`` `