asp.net Mvc 動態建立Controller

來源:互聯網
上載者:User

標籤:prot   路由規則   getc   ted   nbsp   pat   value   bbb   optional   

有這麼個需求,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作為測試,

 

測試結果如下:

 

這樣就簡單的實現了通過博主的需求。。

 

完整工程下載:

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

 

asp.net Mvc 動態建立Controller

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.