protected override object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) { if (parameterDescriptor == null) { throw new ArgumentNullException("parameterDescriptor"); } // Only resolve parameter values if injection is enabled. var context = (AppScope)ZenCoreDependencyResolver.Current.RequestLifetimeScope; var value = context.Scope.ResolveOptional(parameterDescriptor.ParameterType); if (value == null) { // Issue #368 // If injection is enabled and the value can't be resolved, fall back to // the default behavior. Or if injection isn't enabled, fall back. // Unfortunately we can't do much to pre-determine if model binding will succeed // because model binding "knows" about a lot of stuff like arrays, certain generic // collection types, type converters, and other stuff that may or may not fail. try { value = base.GetParameterValue(controllerContext, parameterDescriptor); } catch (MissingMethodException) { // Don't do anything - this means the default model binder couldn't // activate a new instance or figure out some other way to model // bind it. } } return value; }
public static object GetDefaultValue(ParameterDescriptor param) { // If it's a string, giving some value based on the param name if (param.ParameterType == typeof(string)) { return String.Format("Some{0}", param.ParameterName); } // If it's a number, pick some sample number. switch (Type.GetTypeCode(param.ParameterType)) { case TypeCode.Byte: case TypeCode.SByte: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Single: return 7; } // For other value types, go with the default value if (param.ParameterType.IsValueType) { return Activator.CreateInstance(param.ParameterType); } return null; }
private static ParameterDescriptor[] LazilyFetchParametersCollection(ActionDescriptor actionDescriptor, MethodInfo methodInfo, ref ParameterDescriptor[] parametersCache) { return DescriptorUtil.LazilyFetchOrCreateDescriptors<ParameterInfo, ParameterDescriptor>( cacheLocation: ref parametersCache, initializer: methodInfo.GetParameters, converter: parameterInfo => new ReflectedParameterDescriptor(parameterInfo, actionDescriptor)); }
public static ParameterDescriptor[] GetParameters(ActionDescriptor actionDescriptor, MethodInfo methodInfo, ref ParameterDescriptor[] parametersCache) { ParameterDescriptor[] parameters = LazilyFetchParametersCollection(actionDescriptor, methodInfo, ref parametersCache); // need to clone array so that user modifications aren't accidentally stored return (ParameterDescriptor[])parameters.Clone(); }
/// <summary> /// Handles the request. /// </summary> /// <param name="queryString">The query string.</param> /// <param name="queryParams">The query parameters.</param> /// <param name="actionArgs">The action arguments.</param> /// <param name="config">The beetle configuration.</param> /// <param name="request">The request.</param> /// <param name="actionParameters">The action parameters.</param> /// <param name="parameters">The parameters.</param> /// <exception cref="BeetleException">Action must have only one parameter (JsonObject, object or dynamic) to be able called with POST.</exception> internal static void GetParameters(out string queryString, out NameValueCollection queryParams, out object[] actionArgs, BeetleConfig config = null, HttpRequest request = null, ParameterDescriptor[] actionParameters = null, IDictionary<string, object> parameters = null) { if (config == null) config = BeetleConfig.Instance; if (request == null) request = HttpContext.Current.Request; if (request.HttpMethod == "POST") { // supported methods can only have one parameter // (MVC behavior for beetle must be same as WebApi and WebApi cannot have more than one parameters for POST) if (actionParameters == null || actionParameters.Length <= 1) { var hasPrm = actionParameters != null && actionParameters.Length == 1; object actionArg = null; // read post data request.InputStream.Position = 0; queryString = new StreamReader(request.InputStream).ReadToEnd(); if (request.ContentType.Contains("application/json")) { if (hasPrm) actionArg = JsonConvert.DeserializeObject(queryString, config.JsonSerializerSettings) as JObject; // but now there is no query string parameters, we must populate them manually for beetle queries // otherwise beetle cannot use query parameters when using post method var d = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(queryString); queryParams = new NameValueCollection(); foreach (var i in d) queryParams.Add(i.Key, i.Value == null ? string.Empty : i.Value.ToString()); } else { var prms = request.Params; queryParams = prms; var d = prms.AllKeys.ToDictionary(k => k, k => prms[k]); if (hasPrm) { var jsonStr = _javaScriptSerializer.Value.Serialize(d); actionArg = JsonConvert.DeserializeObject(jsonStr, config.JsonSerializerSettings); } } actionArgs = hasPrm ? new[] { actionArg } : new object[0]; } else { queryString = string.Empty; queryParams = null; actionArgs = null; } } else { queryString = request.Url.Query; if (queryString.StartsWith("?")) queryString = queryString.Substring(1); queryString = queryString.Replace(":", "%3A"); queryParams = request.QueryString; if (actionParameters != null && parameters != null) { actionArgs = actionParameters .Select(pd => parameters[pd.ParameterName]) .ToArray(); } else actionArgs = null; } }
private static ParameterDescriptor[] LazilyFetchParametersCollection(ActionDescriptor actionDescriptor, MethodInfo methodInfo, ref ParameterDescriptor[] parametersCache) { // Frequently called, so ensure the delegates remain static return DescriptorUtil.LazilyFetchOrCreateDescriptors( cacheLocation: ref parametersCache, initializer: (CreateDescriptorState state) => state.MethodInfo.GetParameters(), converter: (ParameterInfo parameterInfo, CreateDescriptorState state) => new ReflectedParameterDescriptor(parameterInfo, state.ActionDescriptor), state: new CreateDescriptorState() { ActionDescriptor = actionDescriptor, MethodInfo = methodInfo }); }
private Predicate<string> GetPropertyFilter(ParameterDescriptor parameterDescriptor) { ParameterBindingInfo bindingInfo = parameterDescriptor.BindingInfo; var ba = new BindAttribute() { Exclude = string.Join(",", bindingInfo.Exclude), Include = string.Join(",", bindingInfo.Include) }; return ba.IsPropertyAllowed; }
protected override object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) { if (parameterDescriptor.ParameterType == typeof(IPrincipal)) return HttpContext.Current.User; if (parameterDescriptor.ParameterType == typeof(IUploadedFile)) return new HttpPostedFileUploadedFile(HttpContext.Current.Request.Files[0]); return base.GetParameterValue(controllerContext, parameterDescriptor); }
protected override object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) { if (parameterDescriptor.ParameterType == typeof(IUnitOfWork) || parameterDescriptor.ParameterType == typeof(IDbConnection)) { return null; } return base.GetParameterValue(controllerContext, parameterDescriptor); }
public DirectValueProvider(DirectRequest directRequest, ParameterDescriptor[] parameterDescriptors) { int paramCount = parameterDescriptors.Length; Object[] data = directRequest.Data; DirectMethod directMethod = DirectProvider.GetCurrent().GetMethod(directRequest.Action, directRequest.Method); bool usesNamedArguments = (directMethod != null && directMethod.UsesNamedArguments); if (paramCount > 0) { if (usesNamedArguments) { // named arguments. match params by name var dataObj = data[0] as JObject; for (int i = 0; i < paramCount; i++) { string pName = parameterDescriptors[i].ParameterName; Type pType = parameterDescriptors[i].ParameterType; JToken value = dataObj != null ? dataObj.SelectToken(pName) : null; object rawValue = null; if (value != null && value.Type != JTokenType.Null && value.Type != JTokenType.Undefined) { Type vType = value.GetType(); if (vType == typeof(JObject) && pType != typeof(JObject) || vType == typeof(JArray) && pType != typeof(JArray)) { rawValue = JsonConvert.DeserializeObject(value.ToString(), pType); } else { rawValue = Convert.ChangeType(value.ToString(), pType); } } string attemptedValue = Convert.ToString(rawValue, CultureInfo.InvariantCulture); _values.Add(pName, new ValueProviderResult(rawValue, attemptedValue, CultureInfo.InvariantCulture)); } } else { for (int i = 0; i < parameterDescriptors.Length; i++) { object rawValue = data[i]; if (rawValue != null) { Type vType = rawValue.GetType(); Type pType = parameterDescriptors[i].ParameterType; // Deserialize only objects and arrays and let MVC handle everything else. if (vType == typeof(JObject) && pType != typeof(JObject) || vType == typeof(JArray) && pType != typeof(JArray)) { rawValue = JsonConvert.DeserializeObject(rawValue.ToString(), pType); } } string attemptedValue = Convert.ToString(rawValue, CultureInfo.InvariantCulture); _values.Add(parameterDescriptors[i].ParameterName, new ValueProviderResult(rawValue, attemptedValue, CultureInfo.InvariantCulture)); } } } }
protected override object GetParameterValue(ControllerContext controllerContext,ParameterDescriptor parameterDescriptor) { try { controllerContext.RouteData.DataTokens.Add(typeof(ParameterDescriptor).FullName, parameterDescriptor); return base.GetParameterValue(controllerContext,parameterDescriptor); } finally { controllerContext.RouteData.DataTokens.Remove(typeof(ParameterDescriptor).FullName); } }
protected override object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) { if (parameterDescriptor == null) { throw new ArgumentNullException("parameterDescriptor"); } // Issue #430 // Model binding to collections (specifically IEnumerable<HttpPostedFileBase> // as used in multiple file upload scenarios) breaks if you try to // resolve before allowing default model binding to give it a shot. // You also can't send in an object that needs to be model bound if // it's registered in the container because the container will ignore // the POSTed in values. // // Issue #368 // The original solution to issue #368 was to fall back to default // model binding if the ExtensibleActionInvoker was unable to resolve // a parameter AND if parameter injection was enabled. You can no longer // disable parameter injection, and it turns out for issue #430 that // we need to try model binding first. Unfortunately there's no way // to determine if default model binding will fail, so we give it // a shot and handle what we can. object value = null; try { value = base.GetParameterValue(controllerContext, parameterDescriptor); } catch (MissingMethodException) { // Don't do anything - this means the default model binder couldn't // activate a new instance (like if it's an interface) or figure // out some other way to model bind it. } if (value == null) { // We got nothing from the default model binding, so try to // resolve it. var context = AutofacDependencyResolver.Current.RequestLifetimeScope; value = context.ResolveOptional(parameterDescriptor.ParameterType); } return value; }
protected virtual object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) { // collect all of the necessary binding properties Type parameterType = parameterDescriptor.ParameterType; IModelBinder binder = GetModelBinder(parameterDescriptor); IValueProvider valueProvider = controllerContext.Controller.ValueProvider; string parameterName = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName; Predicate<string> propertyFilter = GetPropertyFilter(parameterDescriptor); // finally, call into the binder ModelBindingContext bindingContext = new ModelBindingContext() { FallbackToEmptyPrefix = (parameterDescriptor.BindingInfo.Prefix == null), // only fall back if prefix not specified ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, parameterType), ModelName = parameterName, ModelState = controllerContext.Controller.ViewData.ModelState, PropertyFilter = propertyFilter, ValueProvider = valueProvider }; object result = binder.BindModel(controllerContext, bindingContext); return result ?? parameterDescriptor.DefaultValue; }
private object GetParameterValue(ParameterDescriptor pd, ActionExecutingContext filterContext) { Type parameterType = pd.ParameterType; IModelBinder binder = pd.BindingInfo.Binder ?? ModelBinders.Binders.GetBinder(pd.ParameterType); IValueProvider valueProvider = filterContext.Controller.ValueProvider; string parameterName = pd.BindingInfo.Prefix ?? pd.ParameterName; Predicate<string> propertyFilter = GetPropertyFilter(pd); ModelBindingContext bindingContext = new ModelBindingContext() { FallbackToEmptyPrefix = (pd.BindingInfo.Prefix == null), ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, parameterType), ModelName = parameterName, ModelState = filterContext.Controller.ViewData.ModelState, PropertyFilter = propertyFilter, ValueProvider = valueProvider }; object result = binder.BindModel(filterContext, bindingContext); return result ?? pd.DefaultValue; }
protected override object GetParameterValue( ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) { object parameter = null; try { parameter = GetParameterValueFromDependencyResolver( parameterDescriptor.ParameterType, parameterDescriptor.ParameterName); } catch { return null; } if (parameter == null) parameter = base.GetParameterValue(controllerContext, parameterDescriptor); return parameter; }
private IModelBinder GetModelBinder(ParameterDescriptor parameterDescriptor) { MethodInfo getModelBinder = typeof(ControllerActionInvoker).GetMethod("GetModelBinder", BindingFlags.Instance | BindingFlags.NonPublic); return (IModelBinder)getModelBinder.Invoke(this.ActionInvoker, new object[] { parameterDescriptor }); }
protected override object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) { var parameterValue = base.GetParameterValue(controllerContext, parameterDescriptor); return parameterValue; }
public DescribedMvcActionParameterInfo(ParameterDescriptor paramDescr, ActionInfo action) : base(action) { this.paramDescr = paramDescr; this.reflectedParamDescr = paramDescr as ReflectedParameterDescriptor; }
protected override object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) { _log.Debug("VerboseControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)"); return base.GetParameterValue(controllerContext, parameterDescriptor); }
private static Predicate <string> GetPropertyFilter(ParameterDescriptor parameterDescriptor) { ParameterBindingInfo bindingInfo = parameterDescriptor.BindingInfo; return(propertyName => BindAttribute.IsPropertyAllowed(propertyName, bindingInfo.Include.ToArray(), bindingInfo.Exclude.ToArray())); }
private IModelBinder GetModelBinder(ParameterDescriptor parameterDescriptor) { // look on the parameter itself, then look in the global table return(parameterDescriptor.BindingInfo.Binder ?? Binders.GetBinder(parameterDescriptor.ParameterType)); }
protected override object GetParameterValue(ControllerContext cc, ParameterDescriptor pd) { var r = base.GetParameterValue(cc, pd); return r; }
protected virtual object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) { // collect all of the necessary binding properties Type parameterType = parameterDescriptor.ParameterType; IModelBinder binder = GetModelBinder(parameterDescriptor); IValueProvider valueProvider = controllerContext.Controller.ValueProvider; string parameterName = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName; Predicate <string> propertyFilter = GetPropertyFilter(parameterDescriptor); // finally, call into the binder ModelBindingContext bindingContext = new ModelBindingContext() { FallbackToEmptyPrefix = (parameterDescriptor.BindingInfo.Prefix == null), // only fall back if prefix not specified ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, parameterType), ModelName = parameterName, ModelState = controllerContext.Controller.ViewData.ModelState, PropertyFilter = propertyFilter, ValueProvider = valueProvider }; object result = binder.BindModel(controllerContext, bindingContext); return(result ?? parameterDescriptor.DefaultValue); }
/// <summary> /// Gets the value of the specified action-method parameter. /// </summary> public virtual object GetParameterValue(ParameterDescriptor parameterDescriptor) { Type parameterType = parameterDescriptor.ParameterType; IModelBinder modelBinder = GetModelBinder(parameterDescriptor); IValueProvider valueProvider = ControllerContext.Controller.ValueProvider; string str = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName; Predicate<string> propertyFilter = GetPropertyFilter(parameterDescriptor); ModelBindingContext bindingContext = new ModelBindingContext() { FallbackToEmptyPrefix = parameterDescriptor.BindingInfo.Prefix == null, ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType((Func<object>)null, parameterType), ModelName = str, ModelState = ControllerContext.Controller.ViewData.ModelState, PropertyFilter = propertyFilter, ValueProvider = valueProvider }; return modelBinder.BindModel(ControllerContext, bindingContext) ?? parameterDescriptor.DefaultValue; }
public virtual IModelBinder GetModelBinder(ParameterDescriptor parameterDescriptor) { return parameterDescriptor.BindingInfo.Binder ?? _modelBinders.GetBinder(parameterDescriptor.ParameterType); }
public Parameter(ParameterDescriptor descriptor) : base(descriptor.ParameterType) { Descriptor = descriptor; }
protected virtual object GetJsonDictValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) { var temp = _jsonRequest.Paramters[parameterDescriptor.ParameterName]; return JsonConverter.DeserializeObject(parameterDescriptor.ParameterType, temp.ToString()); }
protected virtual object GetJsonParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) { return _jsonRequest.GetPostData(parameterDescriptor.ParameterType); }
/// <summary> /// 获取Action的参数签名 /// </summary> /// <param name="args">The args.</param> /// <returns></returns> private string GetActionParamsString(ParameterDescriptor[] args) { StringBuilder sb = new StringBuilder(); bool isFirst = true; foreach (var arg in args) { if (isFirst) isFirst = false; else sb.Append(','); var argType = arg.ParameterType.ToString(); sb.AppendFormat("{0}:{1}", argType.Substring(argType.LastIndexOf('.') + 1), arg.ParameterName); } //无参数Action则用Empty表示 if (sb.Length == 0) sb.Append("Empty"); return sb.ToString(); }
private static Predicate<string> GetPropertyFilter(ParameterDescriptor parameterDescriptor) { ParameterBindingInfo bindingInfo = parameterDescriptor.BindingInfo; return propertyName => BindAttribute.IsPropertyAllowed(propertyName, bindingInfo.Include.ToArray(), bindingInfo.Exclude.ToArray()); }
private IModelBinder GetModelBinder(ParameterDescriptor parameterDescriptor) { // look on the parameter itself, then look in the global table return parameterDescriptor.BindingInfo.Binder ?? Binders.GetBinder(parameterDescriptor.ParameterType); }
private IModelBinder GetModelBinder(ParameterDescriptor parameterDescriptor) { return parameterDescriptor.BindingInfo.Binder ?? Binders.GetBinder(parameterDescriptor.ParameterType); }