c# - ASP.NET MVC 6 Controller Factory -


i want create controllers action database (asp.net mvc 6 vnext). have table controller , actions action table has properties { viewpath, actionname } actionname {controller}/{actionname} want build pages this. how can make it? have class mvc 4 need rewrite mvc 6

public class itsdefaultcontroller : defaultcontrollerfactory     {          public override icontroller createcontroller(system.web.routing.requestcontext requestcontext, string controllername)         {             try             {                 return base.createcontroller(requestcontext, controllername) controller;              }             catch (exception)             {                 controller controller = new itscontrollerbase();                 using (var db = new its.database.databasedatacontext())                 {                     string action = requestcontext.routedata.values["action"] 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>();     } 

actually have same need replace mvc pipeline components custom ones, , found icontrollerfactory , icontrolleractivator , default implementations still same, experience replace mvc 6 defaultcontrollerfactory customcontrollerfactory, i've done tests on startup class on 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);     } 

and s done, can add extension method iservicecollection interface:

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

after modification can use simple :

    ......     services.addmvc();     services.replaceservice(typeof(icontrolleractivator), typeof(customcontrolleractivator));     services.replaceservice(typeof(icontrollerfactory), typeof(customcontrollerfactory));     ...... 

then can replace component on mvc 6 pipeline;


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -