Example #1
0
        public void GetAllControllers()
        {
            Assembly asm = Assembly.GetExecutingAssembly();

            var controlleractionlist = asm.GetTypes()
                                       .Where(type => typeof(System.Web.Mvc.Controller).IsAssignableFrom(type))
                                       .SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
                                       .Where(m => !m.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Any())
                                       .Select(x => new { Area = x.DeclaringType.Namespace, Controller = x.DeclaringType.Name, Action = x.Name, ReturnType = x.ReturnType.Name, Attributes = String.Join(",", x.GetCustomAttributes().Select(a => a.GetType().Name.Replace("Attribute", ""))) })
                                       .OrderBy(x => x.Controller).ThenBy(x => x.Action).ToList();

            // List<TblController> LstControllerClass = new List<TblController>();

            //TblController ControllerClass = new TblController();

            foreach (var item in controlleractionlist)
            {
                TblController controller = new TblController
                {
                    Action     = item.Action,
                    Attributes = item.Attributes,
                    Name       = item.Controller,
                    ReturnType = item.ReturnType,
                    Area       = item.Area
                };

                AddControllersToDb(controller);
            }
        }
Example #2
0
        private void AddControllersToDb(TblController controller)
        {
            //goes through the tblcontrollers table in db and tries to find if the controller exists
            TblController _Controller = db.TblControllers
                                        .FirstOrDefault(p => p.Name == controller.Name &&
                                                        p.Action == controller.Action &&
                                                        p.Attributes == controller.Attributes &&
                                                        p.ReturnType == controller.ReturnType && p.Area == controller.Area);

            // if there is no similar controller then we add the new controller
            if (_Controller == null)
            {
                db.TblControllers.Add(controller);
                db.SaveChanges();
            }
        }