Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to dynamically compile asp.net mvc to generate Controller

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to dynamically compile asp.net mvc to generate Controller, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

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 independent Controller, then we need to use run-time 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 vs

Click to dynamically compile to generate TestController

Visit TestController

Test Action with View ");} 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: "+"

"); foreach (string output in compiler.Output) {sb.Append (output +"

");} sb.Append (" error message: "+"

"); foreach (CompilerError error in compiler.Errors) {sb.Append (error.ErrorText +"

");} # endregion return Content (sb.ToString ());} / dynamically compile and execute the code / the path to the output dll / returns the output private CompilerResults 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 to generate output paras.GenerateInMemory = false; / / whether to generate 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. Accessing TestController will prompt an error of 404.

Then click dynamically compile TestController to generate dll to the bin directory. When you click to visit TestController again, you will be able to access it.

Thank you for reading this article carefully. I hope the article "how to dynamically compile asp.net mvc to generate Controller" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report