前言
在使用时需要修改命名空间、需要过滤控制器、需要过滤Action方法。
结果生成表的插入语句。
代码
public ActionResult ReloadData() { #region 获取所有的控制器类 System.Text.StringBuilder sbSql = new System.Text.StringBuilder(); List<Type> controllerTypes = new List<Type>(); //创建控制器类型列表 var assembly = System.Reflection.Assembly.Load("Qshds.Controllers.Manage"); //加载程序集 controllerTypes.AddRange(assembly.GetTypes().Where(type => typeof(IController).IsAssignableFrom(type) && type.Name != "ErrorController")); //获取程序集下所有的类,通过Linq筛选继承IController类的所有类型 foreach (var controller in controllerTypes)//遍历控制器类 { var actions = controller.GetMethods().Where(method => method.ReturnType.Name == "ActionResult"); //获取控制器下所有返回类型为ActionResult的方法,对MVC的权限控制只要限制所以的前后台交互请求就行,统一为ActionResult foreach (var action in actions) { var catt = action.CustomAttributes.Where(x => x.AttributeType.Name == "FunctionFilter").ToList(); if (catt.Count > 0) { var s1 = catt[0].NamedArguments[0].TypedValue; var s2 = catt[0].NamedArguments[1].TypedValue; var s3 = string.Format("/{0}/{1}/", controller.Name.Replace("Controller", ""), action.Name); sbSql.AppendFormat("INSERT INTO A_GetAllAction(NAME,Code,Url,Sort,ActionType,[CHECK],InsertTime) VALUES('{0}','{1}','{2}',0,0,1,getdate())" , s2.ToString().Replace(""您当前没有", "").Replace("的权限!"", "") , s1.ToString().Replace(""", ""), s3); } } } var res = sbSql.ToString(); #endregion return new RedirectResult("/Action/" + Request.Url.Query); }
CREATE TABLE [dbo].[A_GetAllAction] ( [ActionID] [SMALLINT] IDENTITY(1,1) NOT NULL, [Name] [NVARCHAR](25) NULL, [Code] [CHAR](6) NULL, [Url] [NVARCHAR](50) NULL, [Sort] [SMALLINT] NULL, [ActionType] [INT] NULL, [Memo] [NVARCHAR](200) NULL, [Check] [SMALLINT] NULL, [InsertTime] [SMALLDATETIME] NULL, [ModifyTime] [SMALLDATETIME] NULL ) ON [PRIMARY]