コード例 #1
0
ファイル: NavConfigHandler.cs プロジェクト: lusionx/hy-frame
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        protected void Post(HttpContext context)
        {
            var u   = new AuthedUser();
            var rep = context.Response;

            try
            {
                var bytes = new byte[context.Request.InputStream.Length];
                context.Request.InputStream.Read(bytes, 0, bytes.Length);
                u.UpdateNodes(System.Text.Encoding.UTF8.GetString(bytes));
                var obj = new HY.Frame.Core.ResResult();
                rep.Write(ObjectExtensions.ToJson(obj));
            }
            catch (Exception e)
            {
                this.Log4().Error("保存出错", e);
                rep.StatusCode = 500;
                var obj = new HY.Frame.Core.ResResult {
                    error = true, msg = e.Message
                };
                rep.Write(ObjectExtensions.ToJson(obj));
            }
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            Init(context);
            var    key      = "HY.Frame.Core.MAgentFactory.PathPairs";
            object instance = null;
            Func <object, object[], object> func = null;
            MethodInfo methodInf = null;

            if (CacheData.Exist(key) && CacheData.Get <List <PathPair> >(key).Any(a => a.Path == Request.Url.AbsolutePath))
            {
                #region 从缓存中取得
                var pp = CacheData.Get <List <PathPair> >(key).First(a => a.Path == Request.Url.AbsolutePath);
                instance  = pp.Instance;
                methodInf = pp.MethodInfo;
                func      = pp.Delegate;
                #endregion
            }
            else
            {
                #region 获得 实例 委托
                var url     = FixUrl();
                var path    = url.Split(new string[] { ".", "/" }, StringSplitOptions.RemoveEmptyEntries);
                var mothod  = path[path.Length - 1];//去掉末尾的 .c
                var clsName = string.Join(".", path.Take(path.Length - 1).ToArray());

                //得到正确的程序集
                Assembly assem = GetAssembly(context.Request.PhysicalApplicationPath, path);
                if (assem == null)
                {
                    Response.StatusCode = 404;
                    Response.Write("没有找到程序集");
                    return;
                }

                instance = GetInstance(assem, clsName);
                if (instance == null)
                {
                    Response.StatusCode = 404;
                    Response.Write("没有找到要访问的类");
                    return;
                }

                methodInf = instance.GetType().GetMethod(mothod, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                if (methodInf == null)
                {
                    Response.StatusCode = 404;
                    Response.Write("没有找到要访问的方法");
                    return;
                }

                if (!methodInf.GetCustomAttributes(false).Any(a => a is WebApiAttribute) &&
                    methodInf.ReturnType != typeof(ResResult))
                {
                    Response.StatusCode = 403;
                    Response.Write("拒绝访问方法");
                    return;
                }

                func = GetDelegate(methodInf);
                var pp = new PathPair {
                    Path = Request.Url.AbsolutePath, Delegate = func, Instance = instance, MethodInfo = methodInf
                };
                if (!CacheData.Exist(key))
                {
                    CacheData.Add(key, new List <PathPair> {
                        pp
                    });
                }
                else
                {
                    CacheData.Get <List <PathPair> >(key).Add(pp);
                }
                #endregion
            }

            object[] parVs  = GetParameterValue(methodInf).ToArray();
            object   result = null;
            try
            {
                var handlers = from a in methodInf.GetCustomAttributes(true)
                               where a is ActionBeforeHandlerAttribute
                               select(a as ActionHandlerAttribute);

                foreach (var handler in handlers.ToList())
                {
                    handler.ProcessRequest(context);
                }

                result = func(instance, parVs);

                handlers = from a in methodInf.GetCustomAttributes(true)
                           where a is ActionAfterHandlerAttribute
                           select(a as ActionHandlerAttribute);

                foreach (var handler in handlers.ToList())
                {
                    handler.ProcessRequest(context);
                }
            }
            catch (Exception e)
            {
                Logger.Error("Error.c", e);
                Response.StatusCode = 500;
                result = new ResResult {
                    error = true, msg = e.Message
                };
            }

            Response.AppendHeader("Cache-Control", "no-cache");
            Response.AppendHeader("Pragma", "no-cache");
            Response.AppendHeader("Expires", "0");

            ResponseResult(result);
        }