private AjaxMethod GetMethod(XmlNode node) { AjaxMethod m = new AjaxMethod(); m.MethodName = XmlUtil.GetStringAttribute(node, "name", string.Empty); // cache an hour m.CacheMinutes = XmlUtil.GetIntAttribute(node, "cache", 60); m.CacheTest = XmlUtil.GetStringAttribute(node, "cacheTest", string.Empty); m.AjaxType = XmlUtil.GetStringAttribute(node, "type", "Get"); foreach (XmlNode n in node.SelectNodes("ajax:param", nsmgr)) { m.Params.Add(GetParam(n)); } m.Exception = GetException(node.SelectSingleNode("ajax:onException", nsmgr)); return(m); }
private AjaxMethod GetMethod(XmlNode node) { AjaxMethod m = new AjaxMethod(); m.MethodName = XmlUtil.GetStringAttribute(node, "name", string.Empty); // cache an hour m.CacheMinutes = XmlUtil.GetIntAttribute(node, "cache", 60); m.CacheTest = XmlUtil.GetStringAttribute(node, "cacheTest", string.Empty); m.AjaxType = XmlUtil.GetStringAttribute(node, "type", "Get"); foreach (XmlNode n in node.SelectNodes("ajax:param", nsmgr)) { m.Params.Add(GetParam(n)); } m.Exception = GetException(node.SelectSingleNode("ajax:onException", nsmgr)); return m; }
void proc() { JContext jc = JContext.Current; HttpContext context = jc.Context; // set a ajax request token jc.IsAjaxRequest = true; // get querystring string qs = context.Request.Params["querystring"]; if (StringUtil.HasText(qs)) { qs = qs.TrimStart('?'); jc.QueryString.Add(StringUtil.DelimitedEquation2NVCollection("&", qs)); } if (context.Request.UrlReferrer != null) { UrlMappingModule module = UrlMappingModule.Instance; if (module != null) { UrlMappingItem mapping = null; jc.QueryString.Add(module.GetMappedQueryString(context.Request.UrlReferrer.AbsolutePath, out mapping)); if (mapping != null) { NavigationInfo navi = new NavigationInfo(); navi.Set(mapping, UrlMappingModule.GetUrlRequested(context.Request.UrlReferrer.AbsolutePath)); jc.Navigation = navi; // fire url matched event module.OnUrlMatched(); } } } // set view data UrlMappingModule.SetViewData(); string classId = context.Request.Params[CLASS_ID_PARAM]; string methodName = context.Request.Params[METHOD_NAME_PARAM]; string methodJsonArgs = context.Request.Params[METHOD_ARGS_PARAM]; string jsonp = context.Request.Params[JSONP]; object result; int cacheMinutes = -1; if (string.IsNullOrEmpty(classId) || string.IsNullOrEmpty(methodName)) { result = "null"; } else { AjaxConfiguration config = AjaxConfiguration.GetConfig(); AjaxMethod m = null; try { string id = jc.Navigation.Id; if (id.Contains(":")) { id = id.Substring(id.IndexOf(":") + 1); } AjaxClass c = config.FindClass(classId, id); m = config.FindMethod(c, methodName); if (string.Equals("Post", m.AjaxType, StringComparison.InvariantCultureIgnoreCase)) { cacheMinutes = -1; } else if (StringUtil.HasText(m.CacheTest)) { cacheMinutes = methodJsonArgs.Equals(m.CacheTest) ? cacheMinutes : -1; } // before execute BeforeExecuteEventArgs e = new BeforeExecuteEventArgs() { JContext = jc, TypeName = c.Key, MethodName = m.MethodName }; OnBeforeExecute(e); if (e.PreventDefault) { result = e.ReturnValue; goto response; } if (c.Type != null) { result = m.Invoke(c.Type, methodJsonArgs); } else { result = m.Invoke(c.TypeString, methodJsonArgs); } } catch (Exception ex) { LogManager.GetLogger <AjaxController>().Error("ajax handler error." + ExceptionUtil.WriteException(ex)); AjaxServerException ajaxEx = null; if (m != null) { ajaxEx = m.Exception; } if (ajaxEx != null) { result = ajaxEx.ToJson(); } else { result = null; } } } goto response; response: OnAfterExecute(result); ResponseUtil.OutputJson(context.Response, result, cacheMinutes, jsonp); ContentType = context.Response.ContentType; }
public void findAjaxMethods(IEnumerable<Type> types) { foreach (Type t in types) { AjaxClass ac = new AjaxClass(); ac.Id = "gAjax"; ac.Type = t; foreach (MethodInfo mi in t.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance)) { object[] ajaxattrs = mi.GetCustomAttributes(typeof(AjaxMethodAttribute), true); if (ajaxattrs != null && ajaxattrs.Length == 1) { AjaxMethodAttribute am = ajaxattrs[0] as AjaxMethodAttribute; AjaxMethod ajaxMethod = new AjaxMethod(); ajaxMethod.MethodName = mi.Name; ajaxMethod.AjaxType = am.Type.ToString(); ajaxMethod.CacheMinutes = am.CacheMinutes; ajaxMethod.Exception = new AjaxServerException() { Action = am.OnExceptionAction, Parameter = am.OnExceptionParameter }; foreach (ParameterInfo param in mi.GetParameters()) { string paramType = string.Empty; if (param.ParameterType == typeof(long)) paramType = "long"; else if (param.ParameterType == typeof(int)) paramType = "int"; else if (param.ParameterType == typeof(double)) paramType = "double"; else if (param.ParameterType == typeof(bool)) paramType = "bool"; else if (param.ParameterType == typeof(string[])) paramType = "strings"; else if (param.ParameterType == typeof(int[])) paramType = "ints"; else paramType = param.ParameterType.Name; ajaxMethod.Params.Add(new AjaxParam() { ParamType = paramType, ParamName = param.Name }); } ac.Methods.Add(ajaxMethod); } } AjaxConfiguration.ControllerAjax[t] = ac; } }
private static void AppendMethodScript(string className, AjaxMethod m, ref StringBuilder script) { string ps = GetParamsScript(m.Params); if (m.Params.Count > 0) script.AppendFormat("{0}:function({1},cb)", m.MethodName, ps); else script.AppendFormat("{0}:function(cb)", m.MethodName); script.Append("{"); script.AppendFormat("var args=[{0}]; ", ps); script.AppendFormat("return __ajaxManager.getData('{0}','{1}',args,'{2}',cb,'{3}');", className, m.MethodName, m.AjaxType, StringUtil.CombinUrl(JContext.Current.Area.VirtualPath, "ajax.aspx")); script.Append("}"); }