Home > Article > Backend Development > Detailed example of how asp.net mvc dynamically compiles and generates a Controller
This article mainly introduces the method of asp.net mvc dynamic compilation to generate Controller, which has certain reference value. Those who are interested can learn about it
When building a website backend management system, sometimes we need to The user's input configuration dynamically generates some channels. These channels require the use of independent Controllers. In this case, runtime dynamic compilation is required. 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(@" 这个页面是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; } } }
The process is as follows:
When mvc starts, there is only HomeController, and accessing TestController will prompt 404 Error
Then click to dynamically compile TestController and generate dll to the bin directory. . When you click to access TestController again, it is in an accessible state.
During this process, the mvc application will automatically restart. . Because our configuration is only for background use, I don't think it is necessary to dynamically load the dll and let it restart automatically. . I don’t know if it’s right to think so. . Please give me some advice. .
The above is the detailed content of Detailed example of how asp.net mvc dynamically compiles and generates a Controller. For more information, please follow other related articles on the PHP Chinese website!