Ejemplo n.º 1
0
        internal void Init(HttpContext httpContext, ControllerInfo controllerInfo, BeeDataAdapter viewData, string actionName)
        {
            this.httpContext = httpContext;

            Init(controllerInfo, viewData);
            this.currentActionName = actionName;
        }
Ejemplo n.º 2
0
        internal void Init(ControllerInfo controllerInfo, BeeDataAdapter viewData)
        {
            this.controllerInfo = controllerInfo;
            this.dataAdapter    = viewData;

            if (Inited != null)
            {
                Inited(this, null);
            }
        }
Ejemplo n.º 3
0
        public ControllerInfo GetControllerInfo(string controllerName)
        {
            ControllerInfo result = null;

            if (ControllerDict.ContainsKey(controllerName))
            {
                result = ControllerDict[controllerName];
            }

            return(result);
        }
Ejemplo n.º 4
0
        private static void SearchControllers(Assembly assembly)
        {
            foreach (var type in assembly.GetTypes())
            {
                if (!type.IsInterface && !type.IsAbstract && type.IsSubclassOf(CbType) &&
                    !type.IsGenericType &&
                    type.Name.EndsWith("Controller"))
                {
                    var ci = new ControllerInfo(type);

                    if (ci.DefaultFlag)
                    {
                        ControllerManager.DefaultControllerName = ci.LowerName;
                    }

                    ThrowExceptionUtil.ArgumentConditionTrue(!ControllerDict.ContainsKey(ci.LowerName),
                                                             string.Empty, "ControllerName: {0} has existed".FormatWith(ci.LowerName));

                    ControllerDict[ci.LowerName] = ci;
                }
            }
        }
Ejemplo n.º 5
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);
            }
        }