Exemple #1
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //base.OnActionExecuting(filterContext);
            //读取请求上下文中的Controller,Action,Id
            var routes = new RouteCollection();

            RouteConfig.RegisterRoutes(routes);
            RouteData routeData = routes.GetRouteData(filterContext.HttpContext);

            //取出区域的控制器Action,id
            string ctlName = filterContext.Controller.ToString();

            string[] routeInfo  = ctlName.Split('.');
            string   controller = null;
            string   action     = null;
            string   id         = null;

            int iAreas = Array.IndexOf(routeInfo, "Areas");

            if (iAreas > 0)
            {
                Area = routeInfo[iAreas + 1];//取区域及控制器
            }

            int ctlIndex = Array.IndexOf(routeInfo, "Controllers");

            ctlIndex++;
            controller = routeInfo[ctlIndex].Replace("Controller", "").ToLower();

            string url = HttpContext.Current.Request.Url.ToString().ToLower();

            string[] urlArray    = url.Split('/');
            int      urlCtlIndex = Array.IndexOf(urlArray, controller);

            urlCtlIndex++;
            if (urlArray.Count() > urlCtlIndex)
            {
                action = urlArray[urlCtlIndex];
            }
            urlCtlIndex++;
            if (urlArray.Count() > urlCtlIndex)
            {
                id = urlArray[urlCtlIndex];
            }
            //url
            action = string.IsNullOrEmpty(action) ? "index" : action;
            int actionIndex = action.IndexOf("?", 0);

            if (actionIndex > 1)
            {
                action = action.Substring(0, actionIndex);
            }

            id = string.IsNullOrEmpty(id) ? "" : id;

            #region wechat project add
            var actionParameters = filterContext.ActionDescriptor.GetParameters();
            foreach (var p in actionParameters)
            {
                if (p.ParameterType == typeof(string))
                {
                    if (filterContext.ActionParameters[p.ParameterName] != null)
                    {
                        filterContext.ActionParameters[p.ParameterName] = ResultHelper.FormatStr(filterContext.ActionParameters[p.ParameterName].ToString());
                    }
                }
            }
            #endregion

            //URL路径
            string       filePath = HttpContext.Current.Request.FilePath;
            AccountModel account  = filterContext.HttpContext.Session["Account"] as AccountModel;
            if (ValidDataPermission(account, controller, action, filePath))
            {
                return;
            }
            else
            {
                filterContext.Result = new EmptyResult();
                return;
            }
        }