/// <summary> /// 写出异常数据 /// </summary> internal static void Page_Error(object sender, EventArgs e) { AjaxResult result = new AjaxResult(); Exception ex = HttpContext.Current.Server.GetLastError(); result.Success = false; result.Message = ex.Message; result.TagA = ex.ToString(); HttpContext.Current.Server.ClearError(); AjaxUtils.AjaxToPageScript(result); }
/// <summary> /// 在页中注册连接,页必须将 Head 标记注册为: runat=server /// </summary> public static void RegisterPage() { if (HttpContext.Current == null) { throw new Exception("Ajax Error Is Not Form Http Server;"); } Page page = ((Control)HttpContext.Current.CurrentHandler).Page; if (page == null || page.Header == null) { throw new AjaxHeadNotServer(); } AjaxUtils.RegisterPage(page.Header); }
/// <summary> /// 根据指定的属性串得到函数体 /// </summary> /// <param name="type">类型</param> /// <param name="obj">对象</param> private static string MakeFunction(Type type, object obj) { object tmp; Type tp; PropertyInfo[] pi = type.GetProperties(); StringBuilder fun = new StringBuilder(); fun.AppendLine("function(){"); foreach (PropertyInfo p in pi) { if (!p.CanRead) { continue; } tmp = p.GetValue(obj, null); fun.AppendFormat(" this.{0} = ", p.Name); //对bool值的处理 if (tmp == null) { fun.Append("null"); fun.AppendLine(";"); continue; } tp = tmp.GetType(); if (tp == typeof(Boolean)) { fun.Append(tmp.ToString().ToLower()); fun.AppendLine(";"); continue; } if (tp.IsArray)//数组 { fun.Append(MakeArray((object[])tmp)); continue; } if (tp.GetCustomAttributes(typeof(AjaxObject), true).Length > 0) //对象 { fun.Append("new "); fun.Append(MakeFunction(tp, tmp)); continue; } //以外所有输出 fun.AppendFormat("unescape('{0}')", AjaxUtils.AjaxEncode(tmp.ToString())); fun.AppendLine(";"); } fun.AppendLine("}"); return(fun.ToString()); }
/// <summary> /// 根据指定的对象数组返回一个数组体 /// </summary> /// <param name="arr">数组</param> private static string MakeArray(object[] arr) { StringBuilder fun = new StringBuilder(); object tmp; Type type; fun.AppendLine("new Array("); for (int i = 0; i < arr.Length; i++) { tmp = arr[i]; type = tmp.GetType(); if (tmp == null) { fun.AppendLine("null"); } else if (type.IsArray) { fun.Append(MakeArray((object[])tmp)); } else if (type.GetCustomAttributes(typeof(AjaxObject), true).Length > 0) //对象 { fun.Append("new "); //新涵数体 fun.Append(MakeFunction(type, tmp)); } else { fun.AppendFormat("unescape('{0}')", AjaxUtils.AjaxEncode(tmp.ToString())); } if (i == arr.Length - 1) //当等于时不须要再加,防止因最后一个增加所导致的错误 { fun.AppendLine(); } else { fun.AppendLine(","); } } //加上最后一个 fun.AppendLine(" )"); return(fun.ToString()); }
/// <summary> /// 窗体载入 /// </summary> /// <param name="e">事件</param> protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (Request.RequestType.ToLower() != "post") { throw new AjaxException("不支持get获取方法,get method is not supported"); } try { string typeName = AjaxUtils.AjaxUnEncode(Request.Form["Ajax_Aooshi_Namespace"].Trim()); string mtdName = AjaxUtils.AjaxUnEncode(Request.Form["Ajax_Aooshi_MethodName"].Trim()); string mtdObject = AjaxUtils.AjaxUnEncode(Request.Form["Ajax_Aooshi_MethodObject"].Trim()); object obj = null; string[] pms = new string[Request.Form.Count - 3]; Type tempType = Type.GetType(typeName, true); MethodInfo mi = tempType.GetMethod(mtdName); if (mi == null) { throw new HttpException(404, "NOT FIND Method Name!"); } if (mi.IsPublic) { AjaxMethod[] ajaxmethods = (AjaxMethod[])mi.GetCustomAttributes(typeof(AjaxMethod), true); if (ajaxmethods.Length == 0) { throw new AjaxNotFindMethodException("所要调用的函数必须是已注册为AjaxMethod属性的函数;"); } if (ajaxmethods[0].MethodType == AjaxMethodType.Array)//数组型式 { Dictionary <string, string> dic = new Dictionary <string, string>(); for (int i = 3; i < Request.Form.Count; i++) { dic.Add(Request.Form.GetKey(i), Request.Form[i]); } if (mi.IsStatic) { obj = mi.Invoke(null, new object[] { dic }); } else { obj = mi.Invoke(tempType.Module.Assembly.CreateInstance(tempType.FullName), new object[] { dic }); } } else if (ajaxmethods[0].MethodType == AjaxMethodType.Object) //对象型式 { Type mtdt = Type.GetType(mtdObject, true); object mtdo = mtdt.Module.Assembly.CreateInstance(mtdt.FullName); PropertyInfo pinfo; for (int i = 3; i < Request.Form.Count; i++) { pinfo = mtdt.GetProperty(Request.Form.GetKey(i), BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty); if (pinfo != null) { pinfo.SetValue(mtdo, Request.Form[i], null); } } if (mi.IsStatic) { obj = mi.Invoke(null, new object[] { mtdo }); } else { obj = mi.Invoke(tempType.Module.Assembly.CreateInstance(tempType.FullName), new object[] { mtdo }); } } else//参数型式 { for (int i = 3; i < Request.Form.Count; i++) { pms[i - 3] = AjaxUtils.AjaxUnEncode(Request.Form[i]); } if (mi.IsStatic) { obj = mi.Invoke(null, pms); } else { obj = mi.Invoke(tempType.Module.Assembly.CreateInstance(tempType.FullName), pms); } } if (obj != null) { Type type = obj.GetType(); if (type == typeof(XmlDocument)) { WriteXmlDoc((XmlDocument)obj); return; } if (type == typeof(Boolean)) { WriteString(Convert.ToInt32(obj).ToString()); return; } //数组 if (type.IsArray) { AjaxMakeObject.RunatArrayObject(obj, type, Response); return; } //对象处理 if (type.GetCustomAttributes(typeof(AjaxObject), true).Length > 0) { AjaxMakeObject.RunatClassObject(obj, type, Response); return; } WriteString(obj); } else { WriteString(""); } } } catch (System.Exception ex) { throw new HttpException(500, ex.Message, ex.InnerException); } throw new HttpException(404, "NOT FIND PAGE!"); }
static AjaxUtils() { AjaxUtils.CallbackPath = AjaxUtils.GetAppDir() + "AjaxAooshi.aspx"; }