MVC版
ModelBinder:参数绑定值
public class FilterModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (bindingContext == null) throw new ArgumentNullException("bindingContext"); string filter = controllerContext.HttpContext.Request.Form["filter"]; if (string.IsNullOrEmpty(filter)) return null; PromoCodeNewModel_Filter fi = JsonConvert.DeserializeObject<PromoCodeNewModel_Filter>(filter); IFilterable t = JsonConvert.DeserializeObject(filter, bindingContext.ModelType) as IFilterable; if (t == null) return null; if (t.Pager == null) { t.Pager = new Pager(); } return t; } }
ModelBinderProvider:如果很多地方需要标注ModelBinder,就适合使用ModelBinderProvider
public class FilterModelBinderProvider : IModelBinderProvider { public System.Web.Mvc.IModelBinder GetBinder(Type modelType) { if (typeof(IFilterable).IsAssignableFrom(modelType)) { return new FilterModelBinder(); } return null; } }
Core版
ModelBinderProvider:

/// <summary> /// An <see cref="IModelBinderProvider"/> for deserializing the request body using a formatter. /// </summary> public class FilterModelBinderProvider : IModelBinderProvider { private readonly IList<IInputFormatter> formatters; private readonly IHttpRequestStreamReaderFactory readerFactory; public FilterModelBinderProvider(IList<IInputFormatter> formatters, IHttpRequestStreamReaderFactory readerFactory) { this.formatters = formatters; this.readerFactory = readerFactory; } /// <inheritdoc /> public IModelBinder GetBinder(ModelBinderProviderContext context) { if (typeof(IFilterable).IsAssignableFrom(context.Metadata.ModelType)) return new FilterModelBinder(formatters, readerFactory); return null; } }
ModelBinder:

/// <summary> /// An <see cref="IModelBinder"/> which binds models from the request body using an <see cref="IInputFormatter"/> /// when a model has the binding source <see cref="BindingSource.Body"/>. /// </summary> public class FilterModelBinder : IModelBinder { private readonly IList<IInputFormatter> formatters; private readonly IHttpRequestStreamReaderFactory readerFactory; public FilterModelBinder(IList<IInputFormatter> formatters, IHttpRequestStreamReaderFactory readerFactory) { this.formatters = formatters; this.readerFactory = readerFactory; } /// <inheritdoc /> public async Task BindModelAsync(ModelBindingContext bindingContext) { await Task.Run(() => { if (bindingContext == null) throw new ArgumentNullException("bindingContext"); string filter = bindingContext.HttpContext.Request.Form["filter"]; if (string.IsNullOrEmpty(filter)) return; IFilterable t = JsonConvert.DeserializeObject(filter, bindingContext.ModelType) as IFilterable; if (t == null) return; if (t.Pager == null) { t.Pager = new Pager(); } bindingContext.Result = ModelBindingResult.Success(t); }); } } /// <summary> /// An <see cref="IModelBinderProvider"/> for deserializing the request body using a formatter. /// </summary> public class FilterModelBinderProvider : IModelBinderProvider { private readonly IList<IInputFormatter> formatters; private readonly IHttpRequestStreamReaderFactory readerFactory; public FilterModelBinderProvider(IList<IInputFormatter> formatters, IHttpRequestStreamReaderFactory readerFactory) { this.formatters = formatters; this.readerFactory = readerFactory; } /// <inheritdoc /> public IModelBinder GetBinder(ModelBinderProviderContext context) { if (typeof(IFilterable).IsAssignableFrom(context.Metadata.ModelType)) return new FilterModelBinder(formatters, readerFactory); return null; } }
使用ModelBinder:
public IActionResult ListData([ModelBinder(typeof(FilterModelBinder))]ActiveList_Filter filter) {}
未完待续...