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");        }


 

 

 

 

原创粉丝点击