2013-07-30 34 views
4

我有一个使用asp.net mvc web api的web api应用程序,它在viewmodels中接收一些十进制数。我想为decimal类型创建一个自定义模型联编程序,并让它适用于所有小数数字。我有一个视图模型是这样的:自定义模型绑定器在Asp.Net Web API中的十进制

public class ViewModel 
{ 
    public decimal Factor { get; set; } 
    // other properties 
} 

而前端应用程序可以发送一个JSON与像一个无效的十进制数:457945789654987654897654987.79746579651326549876541326879854

我想响应与400 - Bad Request错误和自定义信息。我尝试创建一个自定义模型绑定器,实现global.asax上的System.Web.Http.ModelBinding.IModelBinder和registring,但不起作用。我想获得它的工作在我的代码的所有小数,看看我的尝试:

public class DecimalValidatorModelBinder : System.Web.Http.ModelBinding.IModelBinder 
{ 
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) 
    { 
     var input = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); 

     if (input != null && !string.IsNullOrEmpty(input.AttemptedValue)) 
     { 
      if (bindingContext.ModelType == typeof(decimal)) 
      { 
       decimal result; 
       if (!decimal.TryParse(input.AttemptedValue, NumberStyles.Number, Thread.CurrentThread.CurrentCulture, out result)) 
       { 
        actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, ErrorHelper.GetInternalErrorList("Invalid decimal number")); 
        return false; 
       } 
      } 
     } 

     return true; //base.BindModel(controllerContext, bindingContext); 
    } 
} 

添加在Application_Start

GlobalConfiguration.Configuration.BindParameter(typeof(decimal), new DecimalValidatorModelBinder()); 

我能做些什么? 谢谢。

+0

你有没有试图指定的操作参数类型前的粘合剂在操作的参数之前将这个? [ModelBinder(typeof(DecimalValidatorModelBinder))] – Fals

+0

这将验证我的小数属性或我的整个对象上post方法吗?我只是想验证我的小数属性在所有帖子中 –

+0

也许这可以解决您的问题http:// stackoverflow。 com/questions/9434848/custom-model-binder-to-bind-nested-property-values – Fals

回答

5

默认情况下,Web API使用媒体类型格式化程序从请求主体读取复杂类型。所以在这种情况下它不通过模型联编程序。

+0

我真的想用asp.net mvc做我的api,而不是asp.net web api haha​​ah ..坦克你迈克,那是我不想知道的,但它是真正。 –

+0

您是否知道我可以用'aorund'来解决这个问题? –

+0

不能用手。媒体类型格式化程序将在任何模型验证发生之前反序列化请求主体...对于JSON,您可能可以在JSON.Net序列化程序上配置某些内容,以更改其默认行为。 –

0

为JSON,您可以创建JsonConverter(如果你是在默认情况下JSON.NET坚持:

public class DoubleConverter : JsonConverter 
{ 
    public override bool CanWrite 
    { 
     get { return false; } 
    } 

    public override bool CanConvert(Type objectType) 
    { 
     return (objectType == typeof(double) || objectType == typeof(double?)); 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     JToken token = JToken.Load(reader); 
     if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) 
     { 
      return token.ToObject<double>(); 
     } 
     if (token.Type == JTokenType.String) 
     { 
      // customize this to suit your needs 
      var wantedSeperator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator; 
      var alternateSeparator = wantedSeperator == "," ? "." : ","; 
      double actualValue; 
      if (double.TryParse(token.ToString().Replace(alternateSeparator, wantedSeperator), NumberStyles.Any, 
       CultureInfo.CurrentCulture, out actualValue)) 
      { 
       return actualValue; 
      } 
      else 
      { 
       throw new JsonSerializationException("Unexpected token value: " + token.ToString()); 
      } 

     } 
     if (token.Type == JTokenType.Null && objectType == typeof(double?)) 
     { 
      return null; 
     } 
     if (token.Type == JTokenType.Boolean) 
     { 
      return token.ToObject<bool>() ? 1 : 0; 
     } 
     throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); 
    } 

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     throw new NotImplementedException("Unnecessary because CanWrite is false. The type will skip the converter."); 
    } 
} 
+0

您在哪里以及如何注册? – deadManN