• ModelBinder、ModelBinderProvider案例


    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;
            }
        }
    View Code

    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;
            }
        }
    View Code

     使用ModelBinder:

     public IActionResult ListData([ModelBinder(typeof(FilterModelBinder))]ActiveList_Filter filter) {} 

    未完待续...

      
  • 相关阅读:
    【HDU】2295 Radar
    【SPOJ】1771 Yet Another NQueen Problem
    【HDU】2222 Keywords Search
    【HDU】3957 Street Fighter
    【HDU】3156 Repair Depots
    【HDU】4210 Sudominoku
    【HDU】3656 Fire station
    fusioncharts for flex3 对于charts 的一些样式:背景透明,背景插入图片等等 .
    FusionCharts参数的详细说明和功能特性
    ASP.NET AJAX入门系列
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/12111563.html
Copyright © 2020-2023  润新知