This article mainly introduced the ASP. NET MVC Dynamic Compiler Generation controller method, has certain reference value, interested can understand
When doing the site management system, sometimes we need to dynamically generate some channels according to the user's input configuration, these channels need to use the independent controller, then need to use to run the dynamic compilation. The code is as follows:
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 (@ "This page is vs generated <br> <a href= '/home/creat ' > click on Dynamic Compile Build Testcontro ller</a><br> <a href= '/test/' > Access testcontroller</a><br> <a href= '/test/withview ' > Test action</a> with View "); } public Contentresult creat () {String cspath = Server.MapPath ("~/testcontroller.cs"); var compiler = Compilerfromcspath ("TestController", Cspath); Compilation #region output compilation information StringBuilder SB = new StringBuilder (); Sb. Append ("CS file path:" + Cspath); Sb. Append ("Compile message:" + "<br>"); foreach (string output in compiler. Output) {sb. Append (output + "<br>"); } sb. Append ("error message:" + "<br>"); foreach (COmpilererror error in compiler. Errors) {sb. Append (Error. ErrorText + "<br>"); } #endregion return Content (sb.) ToString ()); }///<summary>///dynamic compile and Execute code///</summary>//<param name= "Cspath" > Code </param>// /<param name= "DllName" > Output DLL path </param>//<returns> return output </returns> private Compilerresul TS Compilerfromcspath (String dllName, params string[] cspath) {string binpath = Server.MapPath ("~/bin/"); CSharpCodeProvider complier = new CSharpCodeProvider (); Set compilation parameters CompilerParameters paras = new CompilerParameters (); Introduce third-party 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"); Whether the output paras is generated in memory. GenerateInMemory = false; Whether to generate the executable file paras. GenerateExecutable =False paras. outputassembly = binpath + DllName + ". dll"; Compile code compilerresults result = Complier.compileassemblyfromfile (paras, cspath); return result; } }}
The process is as follows:
When MVC starts, only HomeController, Access TestController will prompt for 404 error
Then click Dynamically compile TestController to generate the DLL to the bin directory. When you click to access TestController, you can access the status.
In this process, the MVC application will automatically restart: Because our configuration is only used in the background, I don't feel the need to dynamically load the DLL, let him automatically restart the line. I don't know if that's the right thing. Please enlighten the big hand.