Example #1
0
        private bool ValidateParas(object[] validateList, string paraName, string paraValue)
        {
            if (validateList != null)
            {
                foreach (object item in validateList)
                {
                    RequireAttribute valid = item as RequireAttribute;
                    if (!valid.isValidated && (valid.paraName == paraName || valid.paraName.StartsWith(paraName + ".")))
                    {
                        valid.isValidated = true;                                                          //设置已经验证过此参数,后续可以跳过。
                        if (valid.paraName.StartsWith(paraName + ".") && !string.IsNullOrEmpty(paraValue)) //json字集
                        {
                            paraValue = JsonHelper.GetValue(paraValue, valid.paraName.Substring(paraName.Length + 1));
                        }
                        if (valid.isRequired && string.IsNullOrEmpty(paraValue))
                        {
                            Write(valid.emptyTip, false);
                            return(false);
                        }
                        else if (!string.IsNullOrEmpty(valid.regex) && !string.IsNullOrEmpty(paraValue))
                        {
                            if (paraValue.IndexOf('%') > -1)
                            {
                                paraValue = HttpUtility.UrlDecode(paraValue);
                            }
                            if (!Regex.IsMatch(paraValue, valid.regex))//如果格式错误
                            {
                                Write(valid.regexTip, false);
                                return(false);
                            }
                        }
                    }
                }
            }

            return(true);
        }
Example #2
0
 private bool GetInvokeParas(MethodInfo method, out object[] paras)
 {
     paras = null;
     #region 增加处理参数支持
     ParameterInfo[] piList       = method.GetParameters();
     object[]        validateList = method.GetCustomAttributes(typeof(RequireAttribute), true);
     if (piList != null && piList.Length > 0)
     {
         paras = new object[piList.Length];
         for (int i = 0; i < piList.Length; i++)
         {
             ParameterInfo pi    = piList[i];
             Type          t     = pi.ParameterType;
             object        value = Query <object>(pi.Name, null);
             if (value == null)
             {
                 if (t.IsValueType && t.IsGenericType && t.FullName.StartsWith("System.Nullable"))
                 {
                     continue;
                 }
                 if (t.Name == "HttpPostedFile")
                 {
                     if (Request.Files != null && Request.Files.Count == 1)
                     {
                         value = Request.Files[0];
                     }
                 }
                 else if (piList.Length == 1 && ReflectTool.GetSystemType(ref t) != SysType.Base)//基础值类型
                 {
                     value = GetJson();
                 }
             }
             //检测是否允许为空,是否满足正则格式。
             if (!ValidateParas(validateList, pi.Name, Convert.ToString(value)))
             {
                 return(false);
             }
             try
             {
                 //特殊值处理
                 if (t.Name == "HttpPostedFile" && value is string && Convert.ToString(value) == DocSettings.DocDefaultImg.ToLower())
                 {
                     string path = DocSettings.DefaultImg;
                     if (!string.IsNullOrEmpty(path))
                     {
                         paras[i] = HttpPostedFileExtend.Create(path);
                     }
                 }
                 else
                 {
                     paras[i] = QueryTool.ChangeType(value, t);//类型转换(基础或实体)
                 }
             }
             catch (Exception err)
             {
                 string typeName = t.Name;
                 if (typeName.StartsWith("Nullable"))
                 {
                     typeName = Nullable.GetUnderlyingType(t).Name;
                 }
                 string outMsg = string.Format("[{0} {1} = {2}]  [Error : {3}]", typeName, pi.Name, value, err.Message);
                 Write(outMsg, false);
                 return(false);
             }
         }
     }
     //对未验证过的参数,再进行一次验证。
     foreach (object item in validateList)
     {
         RequireAttribute valid = item as RequireAttribute;
         if (!valid.isValidated)
         {
             if (valid.paraName.IndexOf(',') > -1)
             {
                 foreach (string name in valid.paraName.Split(','))
                 {
                     if (string.IsNullOrEmpty(Query <string>(name)))
                     {
                         Write(string.Format(valid.emptyTip, name), false);
                         return(false);
                     }
                 }
             }
             else if (!ValidateParas(validateList, valid.paraName, Query <string>(valid.paraName)))
             {
                 return(false);
             }
         }
     }
     validateList = null;
     #endregion
     return(true);
 }