コード例 #1
0
        ParameterBinder GetBinderForType(Type type, IFromRouteAttribute routeAttr, out BinderSource source)
        {
            ParameterBinder paramBinder = null;

            source = BinderSource.None;

            if (routeAttr != null &&
                routeAttr.BinderType != null)
            {
                paramBinder = ParameterBinder.GetInstance(null, routeAttr.BinderType);

                if (paramBinder != null)
                {
                    source = BinderSource.Parameter;
                    return(paramBinder);
                }
            }

            type = TypeHelpers.GetNullableUnderlyingType(type);

            if (this.Register.Settings.ParameterBinders.TryGetItem(type, out paramBinder))
            {
                source = BinderSource.Settings;
                return(paramBinder);
            }

            paramBinder = ParameterBinder.GetInstance(type, null);

            if (paramBinder != null)
            {
                source = BinderSource.Type;
            }

            return(paramBinder);
        }
コード例 #2
0
        bool?ParamBind(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            IHttpRouteData routeData = actionContext.ControllerContext.RouteData;
            string         name      = this.Name ?? bindingContext.ModelName;

            object rawValue;

            if (!routeData.Values.TryGetValue(name, out rawValue))
            {
                return(false);
            }

            if (rawValue == null ||
                bindingContext.ModelType.IsInstanceOfType(rawValue))
            {
                bindingContext.Model = rawValue;
                return(true);
            }

            ParameterBinder paramBinder = null;

            object binders;
            IDictionary <string, ParameterBinder> bindersDictionary;

            if (routeData.Route.DataTokens.TryGetValue(DataTokenKeys.ParameterBinders, out binders) &&
                (bindersDictionary = binders as IDictionary <string, ParameterBinder>) != null)
            {
                bindersDictionary.TryGetValue(name, out paramBinder);
            }

            if (paramBinder == null)
            {
                paramBinder = ParameterBinder.GetInstance(bindingContext.ModelType, this.BinderType);
            }

            if (paramBinder == null)
            {
                return(null);
            }

            object parsedValue;
            bool   success = paramBinder.TryBind(Convert.ToString(rawValue, CultureInfo.InvariantCulture), CultureInfo.InvariantCulture, out parsedValue);

            bindingContext.Model = parsedValue;

            return(success);
        }
コード例 #3
0
        bool?ParamBind(ControllerContext controllerContext, ModelBindingContext bindingContext, out object result)
        {
            RouteData routeData = controllerContext.RouteData;
            string    name      = this.Name ?? bindingContext.ModelName;

            object rawValue;

            if (!routeData.Values.TryGetValue(name, out rawValue))
            {
                result = null;
                return(false);
            }

            if (rawValue == null ||
                bindingContext.ModelType.IsInstanceOfType(rawValue))
            {
                result = rawValue;
                return(true);
            }

            ParameterBinder paramBinder = null;

            Route  route = routeData.Route as Route;
            object binders;
            IDictionary <string, ParameterBinder> bindersDictionary;

            if (route != null &&
                route.DataTokens.TryGetValue(DataTokenKeys.ParameterBinders, out binders) &&
                (bindersDictionary = binders as IDictionary <string, ParameterBinder>) != null)
            {
                bindersDictionary.TryGetValue(name, out paramBinder);
            }

            if (paramBinder == null)
            {
                paramBinder = ParameterBinder.GetInstance(bindingContext.ModelType, this.BinderType);
            }

            if (paramBinder == null)
            {
                result = null;
                return(null);
            }

            return(paramBinder.TryBind(Convert.ToString(rawValue, CultureInfo.InvariantCulture), CultureInfo.InvariantCulture, out result));
        }