C# Asp.NETWebAPI中十进制的自定义模型绑定器

C# Asp.NETWebAPI中十进制的自定义模型绑定器,c#,asp.net,asp.net-mvc,rest,asp.net-web-api,C#,Asp.net,Asp.net Mvc,Rest,Asp.net Web Api,我有一个使用asp.net mvc web api的web api应用程序,它在viewmodels中接收一些十进制数字。我想为decimal类型创建一个自定义模型活页夹,并使其适用于所有小数。我有这样一个viewModel: public class ViewModel { public decimal Factor { get; set; } // other properties } 前端应用程序可以发送带有无效十进制数的json,如:45794578965498765489

我有一个使用asp.net mvc web api的web api应用程序,它在viewmodels中接收一些十进制数字。我想为
decimal
类型创建一个自定义模型活页夹,并使其适用于所有小数。我有这样一个viewModel:

public class ViewModel
{
   public decimal Factor { get; set; }
   // other properties
}
前端应用程序可以发送带有无效十进制数的json,如:
457945789654987654897654987.79746579651326549876541326879854

我想用
400-错误请求
错误和自定义消息进行响应。我尝试创建一个自定义模型绑定器,实现
System.Web.Http.ModelBinding.IModelBinder
,并在global.asax上注册,但不起作用。我想让它适用于我代码中的所有小数,看看我尝试了什么:

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);
    }
}
添加到
应用程序\u Start

GlobalConfiguration.Configuration.BindParameter(typeof(decimal), new DecimalValidatorModelBinder());
我能做什么?
谢谢。

默认情况下,Web API使用媒体类型格式化程序从请求正文中读取复杂类型。因此,在这种情况下,它不会通过模型绑定器

对于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.");
    }
}
公共类双转换器:JsonConverter
{
公共覆盖布尔可写
{
获取{return false;}
}
公共覆盖布尔CanConvert(类型objectType)
{
返回(objectType==typeof(double)| | objectType==typeof(double?);
}
公共重写对象ReadJson(JsonReader阅读器,类型objectType,对象existingValue,JsonSerializer序列化程序)
{
JToken令牌=JToken.Load(读卡器);
if(token.Type==JTokenType.Float | | token.Type==JTokenType.Integer)
{
返回token.ToObject();
}
if(token.Type==JTokenType.String)
{
//定制此选项以满足您的需要
var WantedSeparator=NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
var alternateSeparator=WantedSeparator==”,“?”:“,”;
双重实际价值;
if(double.TryParse(token.ToString().Replace)(AlternateSparator,WantedSeparator),NumberStyles.Any,
CultureInfo.CurrentCulture,输出实际值)
{
返回实际值;
}
其他的
{
抛出新的JsonSerializationException(“意外的令牌值:+token.ToString());
}
}
if(token.Type==JTokenType.Null&&objectType==typeof(double?)
{
返回null;
}
if(token.Type==JTokenType.Boolean)
{
返回token.ToObject()?1:0;
}
抛出新的JsonSerializationException(“意外的令牌类型:+token.type.ToString());
}
公共重写void WriteJson(JsonWriter编写器、对象值、JsonSerializer序列化器)
{
抛出new NotImplementedException(“不必要,因为CanWrite为false。该类型将跳过转换器。”);
}
}

是否尝试在操作参数类型之前指定绑定器?请将其置于操作[ModelBinder(typeof(DecimalValidatorModelBinder))]的参数之前它将在post方法上验证我的decimal属性或我的整个对象?我只想在所有post中验证我的decimal属性。也许这可以解决您的问题,到底什么不起作用?模型绑定器从未调用过,ValueProvider是否返回值,或者只是没有值绑定到方法参数?我通常使用e
IModelBinder
来自
System.Web.Mvc
,而不是
System.Web.Http.ModelBinding
,然后在我的Global.asax文件中使用以下代码:
ModelBinders.Binders.Add(typeof(decimal),new DecimalValidatorModelBinder()
我真的在考虑用asp.net mvc作为我的api,而不是asp.net web api哈哈哈..迈克,这是我不想知道的,但这是真的。你知道我能为
word aorund
做什么解决方案吗?不是马上就做的。媒体类型格式化程序将在任何模型验证发生之前反序列化请求体…对于J儿子,您可以在JSON.Net序列化程序上配置一些东西,以更改其默认行为。您在何处以及如何注册它?