Beispiel #1
0
        public DextopApiInvocationResult Invoke(string action, string[] arguments, DextopFormSubmit form)
        {
            var method = controller.GetType().GetMethod(action);

            if (method == null)
            {
                throw new DextopException("Cannot find method '{0}' in controller type '{1}'.", action, controller.GetType());
            }

            var parameters = method.GetParameters();
            var p          = new object[parameters.Length];

            int offset = form == null ? 0 : 1;

            if (form != null)
            {
                p[0] = form;
            }

            for (var i = 0; i < Math.Min(p.Length, arguments.Length); i++)
            {
                p[i + offset] = DextopUtil.DecodeValue(arguments[i], parameters[i + offset].ParameterType);
            }

            try
            {
                var value = method.Invoke(controller, p);
                return(DextopApiInvocationResult.Success(value));
            }
            catch (Exception ex)
            {
                return(DextopApiInvocationResult.Exception(ex));
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            var ajaxRequest = context.Request.QueryString["ajax"] == "1";

            if (ajaxRequest)
            {
                ProcessAjaxRequest(context);
                return;
            }

            var formSubmit = context.Request.QueryString["formSubmit"] == "1";
            var upload     = formSubmit && context.Request.Form["extUpload"] == "true";

            var requests = upload ? GetUploadRequest(context) : GetActionRequest(context);

            var responses = new List <Response>();

            foreach (var request in requests)
            {
                DextopApiInvocationResult result;
                try
                {
                    using (var apiContext = DextopApi.Resolve <DextopApiContext>())
                    {
                        var controllerType = Type.GetType(request.data[0]);
                        apiContext.Scope       = DextopUtil.Decode <DextopConfig>(request.data[1]);
                        apiContext.HttpContext = new HttpContextWrapper(context);
                        var controller = apiContext.ResolveController(controllerType);
                        controller.OnInitialize();

                        try
                        {
                            controller.OnExecuting();
                            result = controller.Invoke(request.data[2], request.FormSubmit, DextopUtil.Decode <string[]>(request.data[3]));
                            controller.OnExecuted();
                        }
                        catch (Exception ex)
                        {
                            controller.OnError(ex);
                            throw;
                        }
                    }
                }
                catch (Exception ex)
                {
                    result = DextopApiInvocationResult.Exception(ex);
                }

                var response = new Response
                {
                    action = request.action,
                    method = request.method,
                    type   = request.type,
                    tid    = request.tid,
                    result = result
                };

                responses.Add(response);
            }

            DextopUtil.Encode(responses, context.Response.Output);
        }