2013-07-30 38 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