public static bool CanSupportDynamicProxy(this IModelBinder modelBinder)
        {
            var warnings = HttpContext.Current.GetWarnings();

            if (modelBinder is DefaultModelBinder)
            {
                //Make sure there is a parameterless constructor and the type is not sealed
                var modelBinderType    = modelBinder.GetType();
                var proxy              = modelBinder as IProxyTargetAccessor;
                var defaultConstructor = modelBinderType.GetConstructor(new Type[] { });

                var result = (!modelBinderType.IsSealed &&
                              defaultConstructor != null &&
                              proxy == null);

                if (!result)
                {
                    warnings.Add(new NotProxyableWarning(modelBinder));
                }

                return(result);
            }

            warnings.Add(new NotADefaultModelBinderWarning(modelBinder));
            return(false);
        }
Esempio n. 2
0
 public void ChoseModelBinder(Type modelType, IModelBinder binder)
 {
     _report.AddBindingDetail(new ModelBinderSelection{
         ModelType = modelType,
         BinderType = binder.GetType()
     });
 }
Esempio n. 3
0
 public void ChoseModelBinder(Type modelType, IModelBinder binder)
 {
     _report.AddBindingDetail(new ModelBinderSelection {
         ModelType  = modelType,
         BinderType = binder.GetType()
     });
 }
        public static IModelBinder CreateDynamicProxy(this IModelBinder modelBinder)
        {
            var proxyGenerator  = new ProxyGenerator();
            var proxyGenOptions = new ProxyGenerationOptions(new ModelBinderProxyGenerationHook())
            {
                Selector = new ModelBinderInterceptorSelector()
            };

            return((IModelBinder)proxyGenerator.CreateClassProxy(modelBinder.GetType(), proxyGenOptions, new BindModelInterceptor(), new BindPropertyInterceptor(), new CreateModelInterceptor()));
        }
Esempio n. 5
0
        public void DefaultBinderIsInstanceOfDefaultModelBinder()
        {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act
            IModelBinder defaultBinder = binders.DefaultBinder;

            // Assert
            Assert.AreEqual(typeof(DefaultModelBinder), defaultBinder.GetType());
        }
        public ActionResult ShowModelBinder(ModelBindDemo modelBindDemo)
        {
            IModelBinder binding = ModelBinders.Binders.GetBinder(typeof(ModelBindDemo));

            ResultModel result = new ResultModel
            {
                Content = $"modelBindDemo:{binding.GetType().Name}"
            };

            return(View("Content", result));
        }
Esempio n. 7
0
        internal static bool CanSupportDynamicProxy(this IModelBinder modelBinder, IGlimpseLogger logger)
        {
            if (modelBinder is DefaultModelBinder)
            {
                //Make sure there is a parameterless constructor and the type is not sealed
                var modelBinderType    = modelBinder.GetType();
                var proxy              = modelBinder as IProxyTargetAccessor;
                var defaultConstructor = modelBinderType.GetConstructor(new Type[] { });

                var result = (!modelBinderType.IsSealed &&
                              defaultConstructor != null &&
                              proxy == null);

                if (!result)
                {
                    logger.Warn("Cannot create proxy of " + modelBinder.GetType() + ". Object must have a parameterless constructor, cannot be sealed, and cannot already be a proxy object.");
                }

                return(result);
            }

            logger.Warn(modelBinder.GetType() + " is not a System.Web.Mvc.DefaultModelBinder.");
            return(false);
        }
Esempio n. 8
0
        internal static IModelBinder CreateDynamicProxy(this IModelBinder modelBinder, IGlimpseLogger logger)
        {
            var proxyConfig = new Dictionary <string, IInterceptor>
            {
                { "BindModel", new BindModelInterceptor() },
                { "BindProperty", new BindPropertyInterceptor() },
                { "CreateModel", new CreateModelInterceptor() },
            };

            var proxyGenerator  = new ProxyGenerator();
            var proxyGenOptions = new ProxyGenerationOptions(new SimpleProxyGenerationHook(logger, proxyConfig.Keys.ToArray()))
            {
                Selector = new SimpleInterceptorSelector(proxyConfig)
            };

            return((IModelBinder)proxyGenerator.CreateClassProxy(modelBinder.GetType(), proxyGenOptions, proxyConfig.Values.ToArray()));
        }
 public NotADefaultModelBinderWarning(IModelBinder modelBinder)
 {
     Message = modelBinder.GetType() + " is not a System.Web.Mvc.DefaultModelBinder.";
 }
 public IModelBinder GetBinder(ModelBinderProviderContext context) =>
 context.BindingInfo.BinderType == _binder.GetType() ?
 _binder :
 TryAndCreate(context.BindingInfo.BinderType);
 public NotADefaultModelBinderWarning(IModelBinder modelBinder)
 {
     Message = modelBinder.GetType() + " is not a System.Web.Mvc.DefaultModelBinder.";
 }
Esempio n. 12
0
        public object Invoke(IModelBinder handler, HttpContext context)
        {
            MethodInfo m = null;

            if (string.IsNullOrEmpty(method))
            {
                // call the request method
                // if no method is passed then well call the method by HTTP verb (GET, POST, DELETE, UPDATE)
                method = context.Request.RequestType.ToUpper();
            }
            m = handler.GetType().GetMethod(method);

            List <object> a = new List <object>();

            if (m == null)
            {
                if (method.ToLower() == "help")
                {
                    m = handler.GetType().BaseType.GetMethod("Help");
                }
                else
                {
                    throw new Exception(string.Format("Method {0} not found", method));
                }
            }
            else
            {
                // evaluate the handler and method attributes against Http allowed verbs

                /*
                 *               The logic here is:
                 *	-> if no attribute is found means it allows every verb
                 *	-> is a method have verb attributes defined then it will ignore the ones on the class
                 *	-> verb attributes on the class are applied to all methods without verb attributes
                 */
                var handlerSupportedVerbs = handler.GetType().GetCustomAttributes(typeof(HttpVerbAttribute), true).Cast <HttpVerbAttribute>();
                var methodSupportedVerbs  = m.GetCustomAttributes(typeof(HttpVerbAttribute), true).Cast <HttpVerbAttribute>();

                bool VerbAllowedOnMethod  = (methodSupportedVerbs.Count() == 0);
                bool VerbAllowedOnHandler = (handlerSupportedVerbs.Count() == 0);
                if (methodSupportedVerbs.Count() > 0)
                {
                    VerbAllowedOnMethod = methodSupportedVerbs.FirstOrDefault(x => x.HttpVerb == context.Request.RequestType.ToUpper()) != null;
                }
                else if (handlerSupportedVerbs.Count() > 0)
                {
                    VerbAllowedOnHandler = handlerSupportedVerbs.FirstOrDefault(x => x.HttpVerb == context.Request.RequestType.ToUpper()) != null;
                }

                if (!VerbAllowedOnMethod || !VerbAllowedOnHandler)
                {
                    throw new HttpVerbNotAllowedException();
                }
            }

            foreach (var param in m.GetParameters())
            {
                a.Add(ProcessProperty(param.Name, param.ParameterType, string.Empty));
            }

            // OnMethodInvoke -> Invoke -> AfterMethodInvoke
            OnMethodInvokeArgs cancelInvoke = new OnMethodInvokeArgs(m);

            handler.OnMethodInvoke(cancelInvoke);

            object invokeResult = null;

            if (!cancelInvoke.Cancel)
            {
                invokeResult = m.Invoke(handler, a.ToArray());
                handler.AfterMethodInvoke(invokeResult);
            }

            return(invokeResult);
        }