private static string GetRawValue(Controller controller, ActionParameterMethod method, string name) { switch (method) { case ActionParameterMethod.Query: return(controller.Request.QueryString[name]); case ActionParameterMethod.Form: return(controller.HasFormData ? controller.GetFormData()[name] : null); case ActionParameterMethod.Body: return(controller.HasEntityBody ? controller.GetEntityBodyString() : null); case ActionParameterMethod.Url: return(controller.UrlSegments[name]); default: return(null); } }
private static Expression GenerateParameterParser(ParameterInfo param, ActionParameterMethod paramMethod, Expression controllerParam) { var methodAttrib = param.GetCustomAttribute <ActionParameterAttribute>(); if (methodAttrib != null) { paramMethod = methodAttrib.Method; } var type = param.ParameterType; var methodName = $"Read{type.Name}"; const BindingFlags flags = BindingFlags.Static | BindingFlags.Public; var method = typeof(ParameterParsers).GetMethod(methodName, flags); if (method == null) { var bodyAttrib = methodAttrib as BodyAttribute; if (bodyAttrib == null || !bodyAttrib.Json) { throw new NotImplementedException(); } method = typeof(ParameterParsers).GetMethod("ReadJson", flags); method = method.MakeGenericMethod(param.ParameterType); } var @default = param.HasDefaultValue ? param.DefaultValue : type.IsValueType ? Activator.CreateInstance(type) : null; var methodConst = Expression.Constant(paramMethod); var nameConst = Expression.Constant(param.Name); var defaultConst = Expression.Constant(@default, type); Expression call = Expression.Call(method, controllerParam, methodConst, nameConst, defaultConst); if (method.ReturnType != param.ParameterType) { call = Expression.Convert(call, param.ParameterType); } return(call); }
public static double ReadDouble(Controller controller, ActionParameterMethod method, string name, double @default) { double value; return(double.TryParse(GetRawValue(controller, method, name), out value) ? value : @default); }
public static float ReadSingle(Controller controller, ActionParameterMethod method, string name, float @default) { float value; return(float.TryParse(GetRawValue(controller, method, name), out value) ? value : @default); }
public static bool ReadBoolean(Controller controller, ActionParameterMethod method, string name, bool @default) { bool value; return(bool.TryParse(GetRawValue(controller, method, name), out value) ? value : @default); }
public static long ReadInt64(Controller controller, ActionParameterMethod method, string name, long @default) { long value; return(long.TryParse(GetRawValue(controller, method, name), out value) ? value : @default); }
public static int ReadInt32(Controller controller, ActionParameterMethod method, string name, int @default) { int value; return(int.TryParse(GetRawValue(controller, method, name), out value) ? value : @default); }
public static string ReadString(Controller controller, ActionParameterMethod method, string name, string @default) { return(GetRawValue(controller, method, name) ?? @default); }
public static object ReadJson <T>(Controller controller, ActionParameterMethod method, string name, T @default) { var str = GetRawValue(controller, method, name); return(!string.IsNullOrEmpty(str) ? JsonConvert.DeserializeObject(str, typeof(T)) : @default); }
public static JArray ReadJArray(Controller controller, ActionParameterMethod method, string name, JArray @default) { var str = GetRawValue(controller, method, name); return(!string.IsNullOrEmpty(str) ? JArray.Parse(str) : @default); }
internal ActionParameterAttribute(ActionParameterMethod method) { Method = method; }