2015-07-13 26 views
6

Tôi muốn tạo bộ điều khiển hành động từ cơ sở dữ liệu (ASP.NET MVC 6 vNext). Tôi có bảng điều khiển và cũng có hành động bảng hành động có các thuộc tính { ViewPath, ActionName } Trường hợp actionName là {Controller}/{ActionName} Tôi muốn tạo các trang như thế này. Làm thế nào tôi có thể làm cho nó? Tôi có lớp cho MVC 4 nhưng tôi cần phải viết lại nó để MVC 6Nhà máy điều khiển ASP.NET 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>(); 
    } 
+0

Có tiến triển nào về chủ đề này không? Tôi sẽ hạnh phúc về một giải pháp. – xforfun

Trả lời

0

thực sự tôi có nhu cầu tương tự để thay thế các thành phần đường ống MVC bởi một số những tùy chỉnh, và tôi phát hiện ra rằng IControllerFactory và IControllerActivator và mặc định của họ triển khai vẫn như nhau, thì kinh nghiệm là để thay thế cho DefaultControllerFactory MVC 6 bởi CustomControllerFactory, tôi đã thực hiện một số xét nghiệm trên lớp khởi động trên ConfigureServices:

public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddMvc(); 
     var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType.FullName.Contains("IControllerFactory")); 
     var serviceIndex = services.IndexOf(serviceDescriptor); 
     services.Insert(serviceIndex, new ServiceDescriptor(typeof(IControllerFactory), typeof(CustomControllerFactory), ServiceLifeTime.Singleton)); 
     services.RemoveAt(serviceIndex + 1); 
    } 

và nó hoàn thành công việc, bạn cũng có thể thêm một phần mở rộng phương thức cho giao diện IServiceCollection:

public static class ServiceCollectionExtensions 
    { 
     public static void(this IServiceCollection services, Type serviceType, Type implementationType, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton) 
     { 
      var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceType); 
      var serviceIndex = services.IndexOf(serviceDescriptor); 
      services.Insert(serviceIndex, new ServiceDescriptor(serviceType, implementationType, serviceLifetime)); 
      services.RemoveAt(serviceIndex + 1); 
     } 
    } 

sau khi sửa đổi này, bạn có thể sử dụng nó như là đơn giản như thế này:

...... 
    services.AddMvc(); 
    services.ReplaceService(typeof(IControllerActivator), typeof(CustomControllerActivator)); 
    services.ReplaceService(typeof(IControllerFactory), typeof(CustomControllerFactory)); 
    ...... 

sau đó bạn có thể thay thế bất kỳ thành phần trên MVC 6 đường ống;

-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()); 
     } 
+0

Giải thích câu trả lời của bạn! –

Các vấn đề liên quan