zoukankan      html  css  js  c++  java
  • 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) {} 

    未完待续...

      
  • 相关阅读:
    【RAY TRACING THE REST OF YOUR LIFE 超详解】 光线追踪 3-7 混合概率密度
    【RAY TRACING THE REST OF YOUR LIFE 超详解】 光线追踪 3-6 直接光源采样
    【RAY TRACING THE REST OF YOUR LIFE 超详解】 光线追踪 3-5 random direction & ONB
    【RAY TRACING THE REST OF YOUR LIFE 超详解】 光线追踪 3-4 基于重要性采样的材质初探
    【RAY TRACING THE REST OF YOUR LIFE 超详解】 光线追踪 3-3 蒙特卡罗 (三)
    区域生长算法 全局分类 C++ & matlab
    智能优化 之 下山单纯形法 C++
    图形暂时停更
    【RAY TRACING THE REST OF YOUR LIFE 超详解】 光线追踪 3-2 蒙特卡罗(二) 重要性采样
    【RAY TRACING THE REST OF YOUR LIFE 超详解】 光线追踪 3-1 蒙特卡罗 (一)
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/12111563.html
Copyright © 2011-2022 走看看