asp.net mvc中利用Assembly实现模块管理
来源:互联网 发布:json时间格式转换 编辑:程序博客网 时间:2024/04/30 12:57
Assembly类在MSDN中的描述:表示一个程序集,它是一个可重用、无版本冲突并且可自我描述的公共语言运行时应用程序构造块。即包含应用程序的所有模块。在Asp.net Mvc中我们可以通过Assembly类来获取所有的Controller和Action信息,存储到数据库中,方便实现模块管理。
以下代码定义了一个Action,用于将Controller和Action同步到数据库对应的表中。
/*代码说明: * ControllerInfo:Model类,存储Controller * ActionInfo:Model类,存储Action * * modelDao:Model操作类 */ public virtual ActionResult SyncControllers() { //禁用所有模块 foreach (var item in modelDao.QueryModel().ToList()) { item.Active = false; } modelDao.ApplyChanges(); //获取包含当前执行的代码的程序集 Assembly assembly = Assembly.GetExecutingAssembly(); //获取程序集中定义的所有类型,返回一个类型数组 var types = assembly.GetTypes(); //遍历类型数组,对所有的控制器进行操作 foreach (var type in types) { if (!type.IsAbstract && type.IsSubclassOf(typeof(System.Web.Mvc.Controller))) { var controllerInfo = modelDao.QueryModel().SingleOrDefault(o => o.Name == type.Name); if (controllerInfo == null) { controllerInfo = new ControllerInfo() { Name = type.Name, }; } var controllerAttributes = type.GetCustomAttributes(true).Cast<Attribute>(); if (controllerInfo.DisplayName == null) { var displayNameAttribute = controllerAttributes.OfType<DisplayNameAttribute>().FirstOrDefault(); controllerInfo.DisplayName = (displayNameAttribute != null) ? displayNameAttribute.DisplayName : controllerInfo.Name; } controllerInfo.Active = true; //禁用Controller下所有Action foreach (var item in controllerInfo.ActionInfoes) { item.Active = false; } /*查找模块下面所有公有Action,刷新数据库Action*/ var methods = type.GetMethods(); foreach (var method in methods) { if (method.IsPublic && ( method.ReturnType.Equals(typeof(System.Web.Mvc.ActionResult)) || method.ReturnType.IsSubclassOf(typeof(System.Web.Mvc.ActionResult)) ) ) { var attributes = method.GetCustomAttributes(true).Cast<Attribute>(); var authorizeExAttribute = attributes.OfType<AuthorizeExAttribute>().FirstOrDefault(); if (authorizeExAttribute != null && !authorizeExAttribute.Hidden) { var actionInfo = controllerInfo.ActionInfoes.SingleOrDefault(o => o.Name == method.Name); if (actionInfo == null) { actionInfo = new ActionInfo() { Name = method.Name }; controllerInfo.ActionInfoes.Add(actionInfo); } var httpPostAttribute = attributes.OfType<HttpPostAttribute>().FirstOrDefault(); var displayNameAttribute = attributes.OfType<DisplayNameAttribute>().FirstOrDefault(); if (displayNameAttribute != null) { actionInfo.DisplayName = displayNameAttribute.DisplayName; } else if (httpPostAttribute == null || string.IsNullOrEmpty(actionInfo.DisplayName)) { actionInfo.DisplayName = actionInfo.Name; } actionInfo.Active = true; } } } //数据库中添加Controller及所有Action if (controllerInfo.ActionInfoes.Count() > 0) { if (controllerInfo.ControllerId == 0) { modelDao.InsertModel(controllerInfo); } modelDao.ApplyChanges(); } } } return Content("Success"); }
- asp.net mvc中利用Assembly实现模块管理
- asp.net mvc中分页--利用PagedList实现
- Asp.net Mvc中利用ValidationAttribute实现xss过滤
- Asp.net Mvc中利用ValidationAttribute实现xss过滤
- asp.net mvc 中利用jquery datatables 实现数据分页显示
- ASP.NET MVC中Area分层模块处理大解密
- ASP.NET MVC中Area分层模块处理大解密
- ASP.NET MVC中Area分层模块处理大解密
- ASP.NET MVC中Area分层模块处理大解密
- ASP.NET MVC 利用IRouteHandler, IHttpHandler实现图片防盗链
- asp.net mvc利用 ajax实现输入自动完成
- ASP.NET MVC 利用IRouteHandler, IHttpHandler实现图片防盗链
- ASP.NET MVC实现权限管理以及权限分配
- ASP.NET Assembly学习心得
- 在ASP.NET中实现MVC模式(一)
- 在ASP.NET中实现MVC模式(二)
- 在ASP.NET中实现MVC模式(三)
- 在ASP.NET中实现MVC模式(四)
- Nielsen:社交、位置、移动下(SOLOMO)的用户行为研究图
- Eclipse/MyEclipse中的代码自动提示功能失效的解决
- 用1、2、2、3、4、5这六个数字,数字排序经典算法
- maven clean install 无法安装JTA之解决
- 收集:Hibernate中常见问题 No row with the given identifier exists问题的原因及解决
- asp.net mvc中利用Assembly实现模块管理
- 分享18个常用的网站性能测试工具
- ”凤毛麟角的ZendFramework“--Zend_Debug的使用和实现1
- oracle 断开用户连接的方法
- session
- java动态实现登录验证
- iOS Dev Center Can't Update Agreement
- 在JS中用JSP中的变量
- Zend Framework 中的类概览