ASP.NET MVC 6控制器工厂

时间:2015-07-13 17:34:52

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我想从数据库创建控制器操作(ASP.NET MVC 6 vNext)。我有表控制器和动作 操作表具有属性{ ViewPath, ActionName }其中actionName为{Controller}/{ActionName} 我想要像这样的构建页面。我该怎么做? 我有MVC 4课程,但我需要将其重写为MVC 6

public class ITSDefaultController : DefaultControllerFactory
    {

        public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            try
            {
                return base.CreateController(requestContext, controllerName) as Controller;

            }
            catch (Exception)
            {
                Controller controller = new ITSControllerBase();
                using (var db = new ITS.Database.DatabaseDataContext())
                {
                    string action = requestContext.RouteData.Values["action"] as string;
                    DynamicAction dynamicAction = null;
                    if (!db.Controllers.Any(x => x.ControllerName == controllerName && x.Views.Any(v => v.ViewName == action)))
                    {
                        dynamicAction = Actions["NotFound"].First();
                        requestContext.RouteData.Values["controller"] = "NotFound";
                        requestContext.RouteData.Values["action"] = "Index";
                    }
                    else
                    {
                        dynamicAction = new DynamicAction()
                        {
                            ActionName = db.Views.First(d => d.ViewName == action && d.Controller.ControllerName == controllerName).ViewName,
                            Result = () => new ViewResult()
                        };
                    }


                    if (dynamicAction != null)
                    {
                        controller.ActionInvoker = new DynamicActionInvoker() { DynamicAction = dynamicAction };
                    }

                    return controller;
                }

            }
        }
        public override void ReleaseController(IController controller)
        {
            base.ReleaseController(controller);
        }
        public static ConcurrentDictionary> Actions = new ConcurrentDictionary>();
    }

1 个答案:

答案 0 :(得分:-1)

     public class HomeController : Controller
        {
            public string _MyName { get; set; }
            // GET: Home
            public ActionResult Index()
            {
                return Content(_MyName);
            }

            public HomeController(string Name)
            {
                _MyName = Name;
            }
        }


 public class MyCustomController : IControllerFactory
    {
        public IController CreateController(RequestContext requestContext, string controllerName)
        {
            HomeController objHomeController = new HomeController("Any thing Which you want to pass/inject.");
            return objHomeController;
        }

        public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
        {
            return SessionStateBehavior.Default;
        }

        public void ReleaseController(IController controller)
        {
            IDisposable disposable = controller as IDisposable;
            if(disposable!=null)
            {
                disposable.Dispose();
            }
        }
    }



 protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            ControllerBuilder.Current.SetControllerFactory(new MyCustomController());
        }