asp.net mvc 動(dòng)態(tài)編譯生成Controller的方法
做網(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)文章
Win7安裝Visual Studio 2015失敗的解決方法
這篇文章主要為大家詳細(xì)介紹了Win7安裝Visual Studio 2015失敗的解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
ASP.NET過(guò)濾HTML字符串方法總結(jié)
這篇文章主要介紹了ASP.NET過(guò)濾HTML字符串方法總結(jié),需要的朋友可以參考下2014-08-08
理解ASP.NET Core 啟動(dòng)類(Startup)
這篇文章主要介紹了ASP.NET Core 啟動(dòng)類(Startup),文中運(yùn)用代碼講解相關(guān)知識(shí)非常詳細(xì),感興趣的小伙伴可以參考一下2021-09-09
CheckBoxList兩列并排編譯為表格顯示具體實(shí)現(xiàn)
CheckBoxList兩列并排的顯示效果相比大家都有見(jiàn)到過(guò)吧,下面是具體的實(shí)現(xiàn)代碼,感興趣的朋友可以參考下哈2013-05-05
Asp.net配合easyui實(shí)現(xiàn)返回json數(shù)據(jù)實(shí)例
這篇文章主要介紹了Asp.net配合easyui實(shí)現(xiàn)返回json數(shù)據(jù)的方法,實(shí)例分析了Asp.net配合easyui返回json數(shù)據(jù)時(shí)出現(xiàn)的問(wèn)題及解決方法,非常具有實(shí)用價(jià)值的技巧,需要的朋友可以參考下2014-12-12
詳解ASP.NET Core部署項(xiàng)目到Ubuntu Server
這篇文章主要介紹了詳解ASP.NET Core部署項(xiàng)目到Ubuntu Server ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
防止在服務(wù)器處理完成之前用戶多次點(diǎn)擊提交按鈕處理代碼
在提交表單時(shí),如果網(wǎng)頁(yè)速度過(guò)慢或者其他原因,用戶多次提交能導(dǎo)致數(shù)據(jù)的修改,怎么解決這個(gè)問(wèn)題呢,接下來(lái)將為您解決這個(gè)問(wèn)題,需要的朋友可以了解下2012-12-12

