An example of how ASP. NET MVC dynamically compiles a method to generate a controller

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.