Exemple #1
0
        internal static string RunAjaxMethod(MethodInfo ajaxMethod, object obj, bool useXml)
        {
            WebAppContext.AjaxContext          = new AjaxContext();
            WebAppContext.AjaxContext.ViewName = WebAppContext.FormData["_VIEWNAME_"];

            try
            {
                object result = ajaxMethod.Invoke(ajaxMethod.IsStatic ? null : obj, CreateParameters(ajaxMethod));

                return(useXml ? (string)result : AjaxHelper.GenerateJSONReturnValue(result));
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException)
                {
                    ex = ExceptionHelper.ResolveTargetInvocationException((TargetInvocationException)ex);
                }

                return(useXml ? AjaxHelper.GenerateXmlError(ex.Message) : AjaxHelper.GenerateJSONError(ex.Message));
            }
        }
Exemple #2
0
        private static void RunAjaxMethod(string pageUrl)
        {
            IHttpResponse response = WebAppContext.Response;

            response.ContentType = "application/json";

            string[] parts = pageUrl.Split('/');

            if (parts.Length != 3)
            {
                throw new ThinkAwayMvcException("Unrecognized Ajax URL");
            }

            string assemblyName = UrlHelper.DecodeFromUrl(parts[0]);
            string className    = UrlHelper.DecodeFromUrl(parts[1]);
            string methodName   = UrlHelper.DecodeFromUrl(parts[2]);

            Type type = Type.GetType(className + ", " + assemblyName);

            if (type == null)
            {
                response.Write(AjaxHelper.GenerateJSONError("Unknown class " + className + " in assembly " + assemblyName));
                return;
            }


            Type       currentType = type;
            MethodInfo method      = null;

            while (method == null && currentType != null && currentType != typeof(object))
            {
                method = currentType.GetMethod(methodName);

                currentType = currentType.BaseType;
            }

            if (method == null)
            {
                response.Write(AjaxHelper.GenerateJSONError("Unknown method " + methodName + " in class " + className));
                return;
            }

            AjaxAttribute[] ajaxAttributes = (AjaxAttribute[])method.GetCustomAttributes(typeof(AjaxAttribute), false);

            if (ajaxAttributes.Length == 0 && !method.IsDefined(typeof(AjaxOkAttribute), true))
            {
                response.Write(AjaxHelper.GenerateJSONError("Method " + methodName + " is not an Ajax method"));
                return;
            }

            bool returnXml = ajaxAttributes.Length > 0 && ajaxAttributes[0].ReturnXml;

            object obj = null;

            if (!method.IsStatic)
            {
                obj = Activator.CreateInstance(type);
            }

            if (returnXml)
            {
                response.ContentType = "text/xml";
            }

            response.Write(WebAppHelper.RunAjaxMethod(method, obj, returnXml));
        }