一、特性类
/// <summary> /// 访问权限控制属性。 /// </summary> [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class ActionAttribute : Attribute { /// <summary> /// 初始化Action类的新实例。 /// </summary> public ActionAttribute(string id) { this.ActionID = id; } /// <summary> /// 操作标识。 /// </summary> public string ActionID { get; set; } }
二、拦截器代码
/// <summary> /// 全局拦截器 /// </summary> protected override void OnAuthorization(AuthorizationContext filterContext) { base.OnAuthorization(filterContext); Type controllerType = filterContext.Controller.GetType(); MethodInfo methodInfo = controllerType.GetMethod(filterContext.ActionDescriptor.ActionName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance); Attribute customAttribute = Attribute.GetCustomAttribute(methodInfo, typeof(GTC.Web.Attributes.ActionAttribute)); GTC.Web.Attributes.ActionAttribute actionAttribute = customAttribute == null ? null : (GTC.Web.Attributes.ActionAttribute)customAttribute; BindCrumbs(actionAttribute.ActionID); } /// <summary> /// 绑定面包屑 /// </summary> /// <param name="functionKey"></param> private void BindCrumbs(string functionKey) { ArrayList arr = new ArrayList(); var thisAction = GetFunctions().FirstOrDefault(p => p.FActionID == functionKey); if (thisAction != null) { arr.Add(thisAction.FActionName); QueryCrumbs(thisAction.FParentActionID, ref arr); arr.Reverse(); ViewBag.Menu = "当前位置 :" + string.Join("", (string[])arr.ToArray(typeof(string))); } } private void QueryCrumbs(string parentId, ref ArrayList arr) { var parentAction = GetFunctions().FirstOrDefault(p => p.FActionID == parentId); if (parentAction != null) { arr.Add("->"); if (parentAction.FParentActionID == "100000") { arr.Add(parentAction.FActionName); } else { arr.Add("<a href='" + parentAction.FActionUrl + "'>" + parentAction.FActionName + "</a>"); QueryCrumbs(parentAction.FParentActionID, ref arr); } } } public static List<Action> GetFunctions() { //获取系统所有权限 }
三、在方法上加标签[Attributes.Action(ActionID = "5201314")]