asp.net mvc 動態編譯產生Controller

來源:互聯網
上載者:User

標籤:asp   generate   out   file   stc   dynamic   UI   returns   build   

做網站後台管理系統的時候,有時我們需要根據使用者的錄入配置動態產生一些頻道,這些頻道需要用到獨立的Controller,這時就需要用到運行時動態編譯了。代碼如下: 

using System.Web.Mvc;using System.CodeDom.Compiler;using System.Text;using Microsoft.CSharp;namespace DynamicCompiler.Controllers{    public class HomeController : Controller    {        // GET: Home        public ContentResult Index()        {            return Content(@"                            這個頁面是vs產生的<br>                             <a href=‘/home/creat‘>點擊動態編譯產生TestController</a><br>                            <a href=‘/Test/‘>訪問TestController</a><br>                            <a href=‘/Test/WithView‘>測試帶View的Action</a>                        ");        }        public ContentResult Creat()        {            string cspath = Server.MapPath("~/TestController.cs");            var compiler = CompilerFromCsPath("TestController", cspath); //編譯            #region 輸出編譯資訊            StringBuilder sb = new StringBuilder();            sb.Append("cs檔案路徑:" + cspath);            sb.Append("編譯資訊:" + "<br>");            foreach (string output in compiler.Output)            {                sb.Append(output + "<br>");            }            sb.Append("錯誤資訊:" + "<br>");            foreach (CompilerError error in compiler.Errors)            {                sb.Append(error.ErrorText + "<br>");            }            #endregion            return Content(sb.ToString());        }        /// <summary>        /// 動態編譯並執行代碼        /// </summary>        /// <param name="csPath">代碼</param>        /// <param name="dllName">輸出dll的路徑</param>        /// <returns>返回輸出內容</returns>        private CompilerResults CompilerFromCsPath(string dllName, params string[] csPath)        {            string binpath = Server.MapPath("~/bin/");            CSharpCodeProvider complier = new CSharpCodeProvider();            //設定編譯參數            CompilerParameters paras = new CompilerParameters();            //引入第三方dll            paras.ReferencedAssemblies.Add("System.dll");            paras.ReferencedAssemblies.Add("System.linq.dll");            paras.ReferencedAssemblies.Add("System.Web.dll");            paras.ReferencedAssemblies.Add(binpath + "System.Web.Mvc.dll");            //是否記憶體中產生輸出            paras.GenerateInMemory = false;            //是否產生可執行檔            paras.GenerateExecutable = false;            paras.OutputAssembly = binpath + dllName + ".dll";            //編譯代碼            CompilerResults result = complier.CompileAssemblyFromFile(paras, csPath);            return result;        }    }}

 

流程如下:

 

mvc啟動的時候,只有HomeController,訪問TestController會提示404錯誤

 

然後點擊動態編譯TestController,產生dll到bin目錄。。再點擊訪問TestController的時候,就是可以訪問的狀態了。

 

 

這過程中,mvc應用程式會自動重啟的。。因為我們的配置僅僅是後台使用,我覺得沒必要再去動態載入dll,讓他自動重啟就行了。。不知道這麼想對不對。。請大手子賜教。。

 

代碼下載:https://files.cnblogs.com/files/wxb8/%E5%8A%A8%E6%80%81%E7%BC%96%E8%AF%91.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.