欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net mvc 動(dòng)態(tài)編譯生成Controller的方法

 更新時(shí)間:2017年08月28日 09:19:40   投稿:zx  
本篇文章主要介紹了asp.net mvc 動(dòng)態(tài)編譯生成Controller的方法,具有一定的參考價(jià)值,有興趣的可以了解一下

做網(wǎng)站后臺(tái)管理系統(tǒng)的時(shí)候,有時(shí)我們需要根據(jù)用戶的錄入配置動(dòng)態(tài)生成一些頻道,這些頻道需要用到獨(dú)立的Controller,這時(shí)就需要用到運(yùn)行時(shí)動(dòng)態(tài)編譯了。代碼如下:

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(@"
              這個(gè)頁(yè)面是vs生成的<br> 
              <a href='/home/creat'>點(diǎn)擊動(dòng)態(tài)編譯生成TestController</a><br>
              <a href='/Test/'>訪問(wèn)TestController</a><br>
              <a href='/Test/WithView'>測(cè)試帶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("錯(cuò)誤信息:" + "<br>");
      foreach (CompilerError error in compiler.Errors)
      {
        sb.Append(error.ErrorText + "<br>");
      }
      #endregion

      return Content(sb.ToString());
    }

    /// <summary>
    /// 動(dòng)態(tài)編譯并執(zhí)行代碼
    /// </summary>
    /// <param name="csPath">代碼</param>
    /// <param name="dllName">輸出dll的路徑</param>
    /// <returns>返回輸出內(nèi)容</returns>
    private CompilerResults CompilerFromCsPath(string dllName, params string[] csPath)
    {
      string binpath = Server.MapPath("~/bin/");
      CSharpCodeProvider complier = new CSharpCodeProvider();
      //設(shè)置編譯參數(shù)
      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");
      //是否內(nèi)存中生成輸出
      paras.GenerateInMemory = false;
      //是否生成可執(zhí)行文件
      paras.GenerateExecutable = false;
      paras.OutputAssembly = binpath + dllName + ".dll";
      //編譯代碼
      CompilerResults result = complier.CompileAssemblyFromFile(paras, csPath);
      return result;
    }
  }
}

流程如下:

 

mvc啟動(dòng)的時(shí)候,只有HomeController,訪問(wèn)TestController會(huì)提示404錯(cuò)誤

 

然后點(diǎn)擊動(dòng)態(tài)編譯TestController,生成dll到bin目錄。。再點(diǎn)擊訪問(wèn)TestController的時(shí)候,就是可以訪問(wèn)的狀態(tài)了。

 

這過(guò)程中,mvc應(yīng)用程序會(huì)自動(dòng)重啟的。。因?yàn)槲覀兊呐渲脙H僅是后臺(tái)使用,我覺(jué)得沒(méi)必要再去動(dòng)態(tài)加載dll,讓他自動(dòng)重啟就行了。。不知道這么想對(duì)不對(duì)。。請(qǐng)大手子賜教。。 

代碼下載:dynamic-Controller_jb51.rar

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論