• asp.net Mvc 动态创建Controller


    有这么个需求,Urls如下:

    http://localhost:52804

    http://localhost:52804/home/test  

    http://localhost:52804/test1

    http://localhost:52804/test1/aaa

    http://localhost:52804/test1/bbb

    http://localhost:52804/test2/aaa

    http://localhost:52804/test2/bbb

    第1和2条url是代表真实存在Controller的,后面的都是需要动态生成Controller的,为了实现这样的需求,通过查找资料,可以重写DefaultControllerFactory实现。步骤和代码如下:

    1、新建FolderControllerFactory.cs继承自DefaultControllerFactory

     public class FolderControllerFactory : DefaultControllerFactory
        {
            public override IController CreateController(RequestContext requestContext, string controllerName)
            {
                var controllerType = GetControllerType(requestContext, controllerName);
                // 如果controller不存在,替换为FolderController
                if (controllerType == null)
                {
                    // 用路由字段动态构建路由变量
                    var dynamicRoute = string.Join("/", requestContext.RouteData.Values.Values);
                    controllerName = "Folder";
                    controllerType = GetControllerType(requestContext, controllerName);
                    requestContext.RouteData.Values["Controller"] = controllerName;
                    requestContext.RouteData.Values["action"] = "Index";
                    requestContext.RouteData.Values["dynamicRoute"] = dynamicRoute;
                }            
                IController controller = GetControllerInstance(requestContext,controllerType);
                return controller;
            }
        }

    2、新建FolderController

    public class FolderController : Controller
        {
            // GET: Folder
            public ActionResult Index(string dynamicRoute)
            {            
                string viewFile = "/Views/" + dynamicRoute + ".cshtml";
                return View(viewFile);
            }
        }

    3、修改路由规则

     public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
                routes.MapRoute(
                    "CatchAll",
                    "{*dynamicRoute}",
                    new { controller = "Folder", action = "Index" }
                );
            }

    4、在Global.asax注册自定义的FolderControllerFactory

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

    5、在Views下新建一些文件夹和View作为测试,如图:

    测试结果如下:

    这样就简单的实现了通过博主的需求。。

    完整工程下载:

    https://files.cnblogs.com/files/wxb8/DynamicController.zip

  • 相关阅读:
    NEC 框架规范 css reset
    NEC 工程师规范
    NEC html规范
    【bzoj2839】【集合计数】容斥原理+线性求阶乘逆元小技巧
    【bzoj1562】【[NOI2009]变换序列】匈牙利算法的性质利用
    【bzoj4808】【马】二分图最大点独立集+简单感性证明
    【hdu1150】【Machine Schedule】二分图最小点覆盖+简单感性证明
    【bzoj4950】【 [Wf2017]Mission Improbable】贪心+二分图匹配
    【bzoj4443】【[Scoi2015]小凸玩矩阵】二分+二分图最大匹配
    【bzoj1977】【严格次小生成树】倍增维护链上最大次大值
  • 原文地址:https://www.cnblogs.com/wxb8/p/7191441.html
Copyright © 2020-2023  润新知