Example #1
0
        /// <summary>
        /// 处理业务逻辑
        /// </summary>
        /// <param name="userToken"></param>
        public override void HttpHandle(IUserToken userToken)
        {
            IHttpResult result;

            try
            {
                this.InitSession(userToken);

                switch (this.Request.Method)
                {
                case ConstHelper.GET:
                case ConstHelper.POST:
                case ConstHelper.PUT:
                case ConstHelper.DELETE:

                    if (this.Request.Query.Count > 0)
                    {
                        foreach (var item in this.Request.Query)
                        {
                            this.Request.Parmas[item.Key] = item.Value;
                        }
                    }
                    if (this.Request.Forms.Count > 0)
                    {
                        foreach (var item in this.Request.Forms)
                        {
                            this.Request.Parmas[item.Key] = item.Value;
                        }
                    }
                    result = GetActionResult();
                    break;

                case ConstHelper.OPTIONS:
                    result = new EmptyResult();
                    break;

                default:
                    result = new ContentResult("不支持的请求方式", System.Net.HttpStatusCode.NotImplemented);
                    break;
                }
            }
            catch (Exception ex)
            {
                result = OnException.Invoke(this, ex);

                if (result == null)
                {
                    result = new ContentResult("请求发生异常:" + ex.Message, System.Net.HttpStatusCode.InternalServerError);
                }
            }

            if (!(result is IBigDataResult))
            {
                Response.SetCached(result, this.Session.CacheCalcString);

                Response.End();
            }
        }
Example #2
0
        /// <summary>
        /// 处理业务逻辑
        /// </summary>
        /// <param name="userToken"></param>
        /// <param name="httpMessage"></param>
        public override void HttpHandle(IUserToken userToken, HttpMessage httpMessage)
        {
            base.HttpHandle(userToken, httpMessage);

            IHttpResult result = null;

            try
            {
                switch (this.Request.Method)
                {
                case ConstHelper.GET:
                case ConstHelper.POST:
                case ConstHelper.PUT:
                case ConstHelper.DELETE:

                    if (this.Request.Query.Count > 0)
                    {
                        foreach (var item in this.Request.Query)
                        {
                            this.Request.Parmas[item.Key] = item.Value;
                        }
                    }
                    if (this.Request.Forms.Count > 0)
                    {
                        foreach (var item in this.Request.Forms)
                        {
                            this.Request.Parmas[item.Key] = item.Value;
                        }
                    }
                    if (OnRequestDelegate != null)
                    {
                        OnRequestDelegate.Invoke(this);
                    }
                    else
                    {
                        result = GetActionResult();
                    }
                    break;

                case ConstHelper.OPTIONS:
                    result = new EmptyResult();
                    break;

                default:
                    result = new ContentResult("不支持的请求方式", System.Net.HttpStatusCode.HttpVersionNotSupported);
                    break;
                }
            }
            catch (Exception ex)
            {
                result = OnException.Invoke(this, ex);

                if (result == null)
                {
                    result = new ContentResult("请求发生异常:" + ex.Message, System.Net.HttpStatusCode.InternalServerError);
                }
            }

            if (result != null)
            {
                if (!(result is IBigDataResult || result is IEventStream))
                {
                    Response.SetCached(result, this.Session.CacheCalcString);

                    Response.End();
                }
            }
        }
Example #3
0
        /// <summary>
        /// MVC处理
        /// </summary>
        /// <param name="routeTable"></param>
        /// <param name="controller"></param>
        /// <param name="actionName"></param>
        /// <param name="nameValues"></param>
        /// <param name="isPost"></param>
        /// <returns></returns>
        public static ActionResult InvokeResult(RouteTable routeTable, Type controller, string actionName, NameValueCollection nameValues, bool isPost)
        {
            if (routeTable == null)
            {
                return(new ContentResult($"o_o,当前未注册任何Controller!", System.Net.HttpStatusCode.NotFound));
            }

            var routing = routeTable.GetOrAdd(controller, actionName, isPost);

            if (routing == null)
            {
                return(new ContentResult($"o_o,找不到:{controller.Name}/{actionName} 当前请求为:{(isPost ? ConstHelper.HTTPPOST : ConstHelper.HTTPGET)}", System.Net.HttpStatusCode.NotFound));
            }

            ActionResult result;

            var goOn = true;

            //类过滤器
            if (routing.FilterAtrrs != null && routing.FilterAtrrs.Any())
            {
                foreach (var arr in routing.FilterAtrrs)
                {
                    var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING);

                    if (method != null)
                    {
                        goOn = (bool)method.Invoke(arr, null);
                    }
                }
            }

            //方法过滤器
            if (routing.ActionFilterAtrrs != null && routing.ActionFilterAtrrs.Any())
            {
                foreach (var arr in routing.ActionFilterAtrrs)
                {
                    var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING);

                    if (method != null)
                    {
                        if ((bool)arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING).Invoke(arr, null) == false)
                        {
                            goOn = false;
                        }
                    }
                }
            }

            #region actionResult

            if (goOn)
            {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.ContentType) &&
                    HttpContext.Current.Request.ContentType.IndexOf(ConstHelper.FORMENCTYPE3, StringComparison.InvariantCultureIgnoreCase) > -1 &&
                    !string.IsNullOrEmpty(HttpContext.Current.Request.Json))
                {
                    try
                    {
                        var dic = SerializeHelper.Deserialize <Dictionary <string, string> >(HttpContext.Current.Request.Json);

                        if (HttpContext.Current.Request.Parmas == null || !HttpContext.Current.Request.Parmas.Any())
                        {
                            HttpContext.Current.Request.Parmas = dic;
                        }
                        else
                        {
                            if (dic != null && dic.Any())
                            {
                                foreach (var item in dic)
                                {
                                    HttpContext.Current.Request.Parmas[item.Key] = item.Value;
                                }
                            }
                        }

                        nameValues = HttpContext.Current.Request.Parmas.ToNameValueCollection();
                    }
                    catch
                    {
                        return(new ContentResult("o_o,错误请求,Json:" + HttpContext.Current.Request.Json, System.Net.HttpStatusCode.BadRequest));
                    }
                }

                result = MethodInvoke(routing.Action, routing.Instance, nameValues);
            }
            else
            {
                result = new EmptyResult();
            }
            #endregion

            var nargs = new object[] { result };

            if (routing.FilterAtrrs != null && routing.FilterAtrrs.Any())
            {
                foreach (var arr in routing.FilterAtrrs)
                {
                    var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTED);
                    if (method != null)
                    {
                        method.Invoke(arr, nargs);

                        result = (ActionResult)nargs[0];
                    }
                }
            }

            if (routing.ActionFilterAtrrs != null && routing.ActionFilterAtrrs.Any())
            {
                foreach (var arr in routing.ActionFilterAtrrs)
                {
                    var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTED);
                    if (method != null)
                    {
                        method.Invoke(arr, nargs);
                        result = (ActionResult)nargs[0];
                    }
                }
            }

            return(result);
        }