Method of dynamically compiling MVC to generate controller


When doing the website background 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. At this time, we need to use the runtime 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 generated by < br > vs 
              To generate testcontroller, click dynamic compile to generate < a > testcontroller
              Access testcontroller < / a > < br > by < A
              Test action with view</a>

    public ContentResult Creat()
      string cspath = Server.MapPath("~/TestController.cs");
      var compiler = CompilerFromCsPath("TestController", cspath); // compile

      #Region output compilation information
      StringBuilder sb = new StringBuilder();
      Sb. Append ("CS file path: + cspath)";

      Sb. Append ("compilation information: +" < 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>");

      return Content(sb.ToString());

    /// <summary>
    ///Compile and execute code dynamically
    /// </summary>
    ///< param name = "cspath" > code < / param >
    ///< param name = "dllname" > path to output DLL < / param >
    ///< returns > returns the output content < / returns >
    private CompilerResults CompilerFromCsPath(string dllName, params string[] csPath)
      string binpath = Server.MapPath("~/bin/");
      CSharpCodeProvider complier = new CSharpCodeProvider();
      //Setting compilation parameters
      CompilerParameters paras = new CompilerParameters();
      //Introduce third party DLL
      paras.ReferencedAssemblies.Add(binpath + "System.Web.Mvc.dll");
      //Generate output in memory
      paras.GenerateInMemory = false;
      //Generate executable
      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 is started, only homecontroller will show 404 errors when accessing testcontroller


Then click dynamic compile testcontroller to generate DLL to bin directory.. When you click again to access testcontroller, you can access it.


In this process, the MVC application will restart automatically.. Because our configuration is only used in the background, I don’t think it’s necessary to dynamically load DLL and let it restart automatically.. I don’t know if that’s right.. Please teach me..

Code download:dynamic-Controller_jb51.rar

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.