Painy James
Painy James

Reputation: 824

Is there any way to create, compile and load dynamically an ASPX page

Ok, I'm wondering if it's possible to create an ASPX page on the fly. I mean, I've done that before*, but I knew beforehand which assembly I was creating, and I got this assembly referenced in the target project in advance (so I just repleace the old by the new one). But now, instead of this, I got to create all the files an ASPX page needs (aspx, codebehind and designer) and add the reference to this reultant assembly to the current executing assembly so I can navigate to this page from there.

So far, I've been able to compile the code behind (the designer seems to be a no-brainer) to an assembly file, thanks to CodeDom, and managed to load this assembly dynamically. But I don't have a clue how can I access this dinamically created page through this assembly, and I guess I'll have to create an .aspx file too (I guess it's not possible to attach the .aspx to the generated assembly).

It might be a wrong approach to what I'm trying to achieve, so I'll be glad if anyone can shed some light uppon this :)

Im adding some code, if that helps anyone:

CodeDomProvider c = CodeDomProvider.CreateProvider("CSharp");

            Assembly executingAssembly = Assembly.GetExecutingAssembly();

            CompilerParameters cp = new CompilerParameters();
            cp.ReferencedAssemblies.Add("system.dll");
            cp.ReferencedAssemblies.Add("system.web.dll");
            cp.ReferencedAssemblies.Add("system.data.dll");
            cp.ReferencedAssemblies.Add("system.xml.linq.dll");
            cp.ReferencedAssemblies.Add("system.drawing.dll");

            cp.CompilerOptions = "/t:library";

            cp.OutputAssembly = "paginatal.dll";

            cp.GenerateInMemory = false;

            StringBuilder sb = new StringBuilder(""); //Texto del codigo
            sb.Append("using System;\n");
            sb.Append("using System.Collections.Generic;");
            sb.Append("using System.Web;");
            sb.Append("using System.Web.UI;");
            sb.Append("using System.Web.UI.WebControls;");

            sb.Append("namespace NuevaClase{ \n");
            sb.Append("public partial class NuevaClase : System.Web.UI.Page { \n");
            sb.Append("public String Yuhuu(){\n return \"Yuhuuuu\";\n }\n");
            sb.Append("protected void Page_Load(object sender, EventArgs e){\n");
            sb.Append("");
            sb.Append("} \n");
            sb.Append("} \n");
            sb.Append("}\n");

CompilerResults cr = c.CompileAssemblyFromSource(cp, sb.ToString());

            if (cr.Errors.Count > 0)
            {
                Console.WriteLine("Error");
            }

            Assembly a = cr.CompiledAssembly;
            Assembly.LoadFrom(a.Location);

*What I did was creating three files: .aspx, aspx.cs and aspx.cs.designer; And then referencing them manually in the .csproj file, so I could compile with msbuild the assembly and move it to substitute the former assembly in the target project. This, believe it or not, worked perfectly, but someone thought that this is not the proper approach and now I'm trying the other way around as I told you before.

Upvotes: 0

Views: 446

Answers (1)

Aristos
Aristos

Reputation: 66641

It might be a wrong approach to what I'm trying to achieve, 
 so I'll be glad if anyone can shed some light uppon this :)

I think that you miss your goal.

What is the goal: To show information's, an output to a response.

For that we use asp.net that help us to programmatically create that output.

What you try to do is: to create a page, that create a second page, that make the output. Why is that ? why not direct use the asp.net programming to show your output and your information's, why you do that ? Is the name on the top of the page that you wish to change ?.

The minus to make your page like that.

  1. Possible bugs that you can not handle
  2. Every time you make a new page, the compiler may recompile the full site, or part of the site.
  3. Is very possible to create conflict class that crash your system.

Upvotes: 1

Related Questions