需要用到这个是因为在 ASP.NET web Form 中使用 ashx 的时候没有一个好的模型绑定. 所以我找到了 dll 中自带的. 并且将他封装了一下
代码如下:
- public class ModelBinder
- {
- public static BinderResultModel<T> BindModel<T>() where T : new()
- {
- var dicState = new ModelStateDictionary();
- ModelBindingExecutionContext context = new ModelBindingExecutionContext(new HttpContextWrapper(HttpContext.Current), dicState);
- ModelBindingContext bindingContext = new ModelBindingContext()
- {
- ModelBinderProviders = ModelBinderProviders.Providers,
- ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => Activator.CreateInstance<T>(), typeof(T)),
- ModelState = dicState,
- ValueProvider = new NameValueCollectionValueProvider(context.HttpContext.Request.Params, CultureInfo.CurrentCulture),
- ValidateRequest = true,
- };
- bindingContext.ValidationNode.ValidateAllProperties = true;
- if (ModelBinders.Binders.DefaultBinder.BindModel(context, bindingContext))
- {
- return new BinderResultModel<T>((T)bindingContext.Model, dicState);
- }
- return BinderResultModel<T>.NULL;
- }
- public class BinderResultModel<T> where T : new()
- {
- public static BinderResultModel<T> NULL
- {
- get
- {
- return new BinderResultModel<T>(default(T), null, "返回空对象");
- }
- }
- public bool IsValid { get; }
- public T Data { get; }
- public string ErrorMessage { get { return _errorMsg; } private set { _errorMsg = value; } }
- private ModelStateDictionary _modelStateDic;
- private string _errorMsg = string.Empty;
- #region 构造函数
- public BinderResultModel(T t, ModelStateDictionary modelStateDic)
- {
- IsValid = false;
- Data = t;
- _modelStateDic = modelStateDic;
- if (_modelStateDic != null)
- {
- IsValid = _modelStateDic.IsValid;
- }
- _errorMsg = GetErrorMessage();
- }
- public BinderResultModel(T t, ModelStateDictionary modelStateDic, string errorMsg) : this(t, modelStateDic)
- {
- _errorMsg = errorMsg;
- }
- #endregion
- #region 私有函数
- private string GetErrorMessage()
- {
- if (_modelStateDic is null)
- {
- return string.Empty;
- }
- StringBuilder builder = new StringBuilder();
- foreach (var state in _modelStateDic)
- {
- foreach (var error in state.Value.Errors)
- {
- builder.Append(error.ErrorMessage);
- }
- builder.AppendLine();
- }
- return builder.ToString();
- }
- #endregion
- }
- }
测试时使用的类:
- class Person
- {
- // [BindingBehavior(BindingBehavior.Never)]
- //[Required(ErrorMessage = "姓名为必填")]
- [Display(Name = "姓名")]
- [MinLength(20, ErrorMessage = "最小长度为 20")]
- [RegularExpression("/^/d$/", ErrorMessage = "不满足正则表达式")]
- public string Name { get; set; }
- public int Age { get; set; }
- [BindingBehavior(BindingBehavior.Never)]// 表示从不绑定此属性
- public List<int> List { get; set; }
- public Dictionary<string, string> Dics { get; set; }
- [Required(ErrorMessage = "ma 为必须")]
- public Man ma { get; set; }
- }
- class Man
- {
- [Required(ErrorMessage = "666666666666")]
- public string MMM { get; set; }
- }
使用方式:
- var result = ModelBinder.BindModel<Person>();
- if (result.IsValid)
- {
- // 绑定成功, 并且校验通过 dosomething
- }
- else
- {
- //do something
- }
来源: http://www.bubuko.com/infodetail-3457574.html