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); }
static IModelBinder GetModelBinder(IFromRouteAttribute fromRouteAttr, Type modelType, HttpConfiguration configuration) { modelType = TypeHelpers.GetNullableUnderlyingType(modelType); ModelBinderAttribute modelBinderAttr = null; if (fromRouteAttr != null) { modelBinderAttr = (ModelBinderAttribute)fromRouteAttr; } if (modelBinderAttr == null || modelBinderAttr.BinderType == null) { ModelBinderAttribute modelTypeAttr = modelBinderAttrCache.GetOrAdd(modelType, t => t.GetCustomAttributes(typeof(ModelBinderAttribute), inherit: true) .Cast <ModelBinderAttribute>() .SingleOrDefault() ); if (modelTypeAttr != null && (modelBinderAttr == null || modelTypeAttr.BinderType != null)) { modelBinderAttr = modelTypeAttr; } } if (modelBinderAttr == null) { modelBinderAttr = new ModelBinderAttribute(); } return(modelBinderAttr.GetModelBinder(configuration, modelType)); }
internal RouteParameter CreateRouteParameter(string name, Type type, IFromRouteAttribute routeAttr, bool isOptional, bool isCatchAll) { if (routeAttr != null && routeAttr.Name.HasValue()) { name = routeAttr.Name; } type = TypeHelpers.GetNullableUnderlyingType(type); string constraint = GetConstraintForType(type, routeAttr); BinderSource binderSource; ParameterBinder binder = GetBinderForType(type, routeAttr, out binderSource); if (binder == null && type.IsEnum) { binder = (ParameterBinder)Activator.CreateInstance(typeof(EnumParameterBinder <>).MakeGenericType(type)); } if (constraint.HasValue() && binderSource != BinderSource.Parameter) { binder = null; } return(new RouteParameter(name, type, constraint, isOptional, isCatchAll, binder)); }
static IModelBinder GetModelBinder(IFromRouteAttribute fromRouteAttr, Type modelType, out bool isDefaultBinder) { if (fromRouteAttr != null && fromRouteAttr.BinderType != null) { try { isDefaultBinder = false; return((IModelBinder)Activator.CreateInstance(fromRouteAttr.BinderType)); } catch (Exception ex) { throw new InvalidOperationException("An error occurred when trying to create the IModelBinder '{0}'. Make sure that the binder has a public parameterless constructor.".FormatInvariant(fromRouteAttr.BinderType.FullName), ex); } } modelType = TypeHelpers.GetNullableUnderlyingType(modelType); IModelBinder binder = ModelBinders.Binders.GetBinder(modelType, fallbackToDefault: false); if (binder != null) { isDefaultBinder = false; return(binder); } isDefaultBinder = true; return(ModelBinders.Binders.DefaultBinder); }
static RouteParameter CreateRouteParameter(ActionParameterInfo actionParam) { IFromRouteAttribute routeAttr = actionParam.FromRouteAttribute; bool isCatchAll = (routeAttr != null) ? routeAttr.CatchAll : false; return(actionParam.Action.Controller.CreateRouteParameter(actionParam.Name, actionParam.Type, routeAttr, actionParam.IsOptional, isCatchAll)); }
string GetConstraintForType(Type type, IFromRouteAttribute routeAttr) { string constraint = null; if (routeAttr != null) { constraint = routeAttr.Constraint; } if (constraint == null) { type = TypeHelpers.GetNullableUnderlyingType(type); this.Register.Settings.DefaultConstraints.TryGetValue(type, out constraint); } return(constraint); }
string GetConstraintForType(Type type, IFromRouteAttribute routeAttr) { string constraint = null; if (routeAttr != null) { constraint = routeAttr.Constraint; } if (constraint == null) { type = TypeHelpers.GetNullableUnderlyingType(type); this.Register.Settings.DefaultConstraints.TryGetValue(type, out constraint); } return constraint; }
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; }
RouteParameter CreateRouteParameter(PropertyInfo property, IFromRouteAttribute routeAttr) { return CreateRouteParameter(property.Name, property.PropertyType, routeAttr, isOptional: false, isCatchAll: false); }
internal RouteParameter CreateRouteParameter(string name, Type type, IFromRouteAttribute routeAttr, bool isOptional, bool isCatchAll) { if (routeAttr != null && routeAttr.Name.HasValue()) { name = routeAttr.Name; } type = TypeHelpers.GetNullableUnderlyingType(type); string constraint = GetConstraintForType(type, routeAttr); BinderSource binderSource; ParameterBinder binder = GetBinderForType(type, routeAttr, out binderSource); if (binder == null && type.IsEnum) { binder = (ParameterBinder)Activator.CreateInstance(typeof(EnumParameterBinder<>).MakeGenericType(type)); } if (constraint.HasValue() && binderSource != BinderSource.Parameter) { binder = null; } return new RouteParameter(name, type, constraint, isOptional, isCatchAll, binder); }
static IModelBinder GetModelBinder(IFromRouteAttribute fromRouteAttr, Type modelType, HttpConfiguration configuration) { modelType = TypeHelpers.GetNullableUnderlyingType(modelType); ModelBinderAttribute modelBinderAttr = null; if (fromRouteAttr != null) { modelBinderAttr = (ModelBinderAttribute)fromRouteAttr; } if (modelBinderAttr == null || modelBinderAttr.BinderType == null) { ModelBinderAttribute modelTypeAttr = modelBinderAttrCache.GetOrAdd(modelType, t => t.GetCustomAttributes(typeof(ModelBinderAttribute), inherit: true) .Cast<ModelBinderAttribute>() .SingleOrDefault() ); if (modelTypeAttr != null && (modelBinderAttr == null || modelTypeAttr.BinderType != null)) { modelBinderAttr = modelTypeAttr; } } if (modelBinderAttr == null) { modelBinderAttr = new ModelBinderAttribute(); } return modelBinderAttr.GetModelBinder(configuration, modelType); }
static IModelBinder GetModelBinder(IFromRouteAttribute fromRouteAttr, Type modelType, out bool isDefaultBinder) { if (fromRouteAttr != null && fromRouteAttr.BinderType != null) { try { isDefaultBinder = false; return (IModelBinder)Activator.CreateInstance(fromRouteAttr.BinderType); } catch (Exception ex) { throw new InvalidOperationException("An error occurred when trying to create the IModelBinder '{0}'. Make sure that the binder has a public parameterless constructor.".FormatInvariant(fromRouteAttr.BinderType.FullName), ex); } } modelType = TypeHelpers.GetNullableUnderlyingType(modelType); IModelBinder binder = ModelBinders.Binders.GetBinder(modelType, fallbackToDefault: false); if (binder != null) { isDefaultBinder = false; return binder; } isDefaultBinder = true; return ModelBinders.Binders.DefaultBinder; }
RouteParameter CreateRouteParameter(PropertyInfo property, IFromRouteAttribute routeAttr) { return(CreateRouteParameter(property.Name, property.PropertyType, routeAttr, isOptional: false, isCatchAll: false)); }