예제 #1
0
        protected virtual void ActionError(string controllerName, string actionName, BeeDataAdapter dataAdapter, Exception innerException)
        {
            BeeMvcResult mvcResult = new BeeMvcResult();

            mvcResult.code = 400;
            mvcResult.msg  = innerException.Message;

            var httpException = innerException as HttpException;

            if (httpException != null)
            {
                mvcResult.code = httpException.GetHttpCode();
            }

            var coreException = innerException as CoreException;

            if (coreException != null)
            {
                mvcResult.code = 405;
            }

            Logger.Error("Invoke {0}.{1} error.\r\n{2}".FormatWith(controllerName, actionName, dataAdapter), innerException);

            WriteMvcResult(HttpContextUtil.CurrentHttpContext, mvcResult);
        }
예제 #2
0
        internal static void ExecuteAction(HttpContext httpContext, string controllerName, string actionName, BeeDataAdapter dataAdapter)
        {
            try
            {
                CoreExecuteAction(httpContext, controllerName, actionName, dataAdapter);
            }
            catch (Exception e)
            {
                BeeMvcResult mvcResult = new BeeMvcResult();
                mvcResult.code = 400;
                mvcResult.msg  = e.Message;

                WriteMvcResult(httpContext, mvcResult);
            }
        }
예제 #3
0
        public void ProcessRequest(HttpContext context)
        {
            System.Runtime.Remoting.Messaging.CallContext.HostContext = context;
            Stopwatch stopwatch = new Stopwatch();

            try
            {
                stopwatch.Start();

                BeeDataAdapter routeData = GetRouteData(context);

                string controllerName = routeData[Constants.BeeControllerName] as string;
                string actionName     = routeData[Constants.BeeActionName] as string;


                HttpContext httpContext = context;
                //this.httpContext = context;
                BeeDataAdapter dataAdapter = new BeeDataAdapter(routeData);

                NameValueCollection formParams = httpContext.Request.Form;
                foreach (string key in formParams.Keys)
                {
                    if (!string.IsNullOrEmpty(key))
                    {
                        dataAdapter.Add(key.ToLower(), StringUtil.HtmlEncode(formParams[key]));
                    }
                }

                formParams = httpContext.Request.QueryString;
                foreach (string key in formParams.Keys)
                {
                    if (!string.IsNullOrEmpty(key))
                    {
                        dataAdapter.Add(key.ToLower(), StringUtil.HtmlEncode(formParams[key]));
                    }
                }
                // 解析inputstream
                string json = new StreamReader(httpContext.Request.InputStream).ReadToEnd();
                if (!string.IsNullOrEmpty(json) && json.StartsWith("{"))
                {
                    var jObject = Newtonsoft.Json.Linq.JObject.Parse(json);
                    foreach (var item in jObject)
                    {
                        dataAdapter.Add(item.Key, item.Value);
                    }
                }

                if (LogRequestFlag)
                {
                    BeeDataAdapter cookieData = new BeeDataAdapter();
                    foreach (string key in context.Request.Cookies.AllKeys)
                    {
                        cookieData.Add(key, context.Request.Cookies[key].Value);
                    }

                    Logger.Debug(@"
cookie:{0}
Request:{1}".FormatWith(cookieData.ToString(), dataAdapter.ToString()));
                }

                ActionExecutingArgs args = new ActionExecutingArgs(controllerName, actionName, dataAdapter);
                ActionExecuting(args); // 提供拦截通道
                if (args.Result != ActionExecutingResult.OK)
                {
                    BeeMvcResult mvcResult = new BeeMvcResult();
                    mvcResult.code = 400;
                    if (args.Code > 0)
                    {
                        mvcResult.code = args.Code;
                    }
                    mvcResult.msg = args.Message;

                    WriteMvcResult(httpContext, mvcResult);
                    return;
                }

                InnerExecuteAction(context, controllerName, actionName, dataAdapter);

                stopwatch.Stop();
                if (stopwatch.ElapsedMilliseconds > 5000)
                {
                    Logger.Debug(string.Format("{0}耗时较长, 耗时:{1}ms", context.Request.Url.ToString(), stopwatch.ElapsedMilliseconds));
                }
            }
            catch (Exception e)
            {
                string error = ResourceUtil.ReadToEndFromCache(typeof(MvcDispatcher).Assembly, "Bee.Web.Error.htm", false);

                context.Response.Write(string.Format(error, e.Message, GetFullException(e)));

                Logger.Error(e.Message, e);

                Logger.Log(LogLevel.Core, e.Message, e);
            }
        }
예제 #4
0
        private static void CoreExecuteAction(HttpContext httpContext, string controllerName, string actionName, BeeDataAdapter dataAdapter)
        {
            if (dataAdapter == null)
            {
                dataAdapter = new BeeDataAdapter();
            }

            //HttpContext httpContext = HttpContextUtil.CurrentHttpContext;

            // 加入Area特性
            string areaName = dataAdapter.TryGetValue <string>(Constants.BeeAreaName, string.Empty);

            if (!string.IsNullOrEmpty(areaName))
            {
                controllerName = "{0}|{1}".FormatWith(areaName, controllerName);
            }

            ControllerInfo controllerInfo = ControllerManager.Instance.GetControllerInfo(controllerName);

            if (controllerInfo == null)
            {
                throw new MvcException(string.Format("Cannot find {0} controller.", controllerName));
            }

            ControllerBase instance = controllerInfo.CreateInstance() as ControllerBase;

            //ReflectionUtil.CreateInstance(controllerInfo.Type) as ControllerBase;
            instance.Init(httpContext, controllerInfo, dataAdapter, actionName);

            GeneralUtil.CatchAll(delegate
            {
                instance.OnBeforeAction(actionName, dataAdapter);
            });

            object result = null;

            try
            {
                // 假如不匹配任何方法, 则会抛出CoreException
                result = controllerInfo.Invoke(instance, actionName, dataAdapter);
            }
            catch (Exception e)
            {
                Logger.Error("Invoke {0}.{1} error.\r\n{2}"
                             .FormatWith(controllerName, actionName, dataAdapter), e);

                throw;
            }

            GeneralUtil.CatchAll(delegate
            {
                instance.OnAfterAction(actionName, dataAdapter);
            });


            // 加入ControllerName及ActionName信息
            dataAdapter.Add(Constants.BeeControllerName, controllerName);
            dataAdapter.Add(Constants.BeeActionName, actionName);
            if (result != null)
            {
                ActionResult actionResult = result as ActionResult;
                if (actionResult != null)
                {
                    if (string.IsNullOrEmpty(actionResult.ControllerName))
                    {
                        actionResult.ControllerName = controllerName;
                    }
                    if (string.IsNullOrEmpty(actionResult.ActionName))
                    {
                        actionResult.ActionName = actionName;
                    }

                    actionResult.Init(instance, dataAdapter);

                    try
                    {
                        actionResult.Ouput(httpContext);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("Rend {0}.{1} error.\r\n{2}"
                                     .FormatWith(controllerName, actionName, dataAdapter), ex);
                        throw new MvcException(ex.Message, ex);
                    }
                }
                else
                {
                    BeeMvcResult mvcResult = new BeeMvcResult();
                    mvcResult.data = result;
                    mvcResult.code = 200;

                    WriteMvcResult(httpContext, mvcResult);
                }
            }
            else
            {
                httpContext.Response.AppendHeader("Content-Type", "application/json; charset=utf-8");
                BeeMvcResult mvcResult = new BeeMvcResult();
                mvcResult.code = 200;

                WriteMvcResult(httpContext, mvcResult);
            }
        }
예제 #5
0
        private static void WriteMvcResult(HttpContext httpContext, BeeMvcResult mvcResult)
        {
            httpContext.Response.AppendHeader("Content-Type", "application/json; charset=utf-8");

            httpContext.Response.Write(SerializeUtil.ToJson(mvcResult));
        }