vendredi 13 février 2009

Asp.Net Compilation: Generate C# Class file from "Aspx" Page

As you know, ASP.NET Works like This:



the Default.aspx page and is Code behind class Default.aspx.cs are compiled into One Class! because in page directives (the first line in Default.apsx) you find something like that:

1: CodeBehind="Default.aspx.cs" Inherits="AspxToCsharp._Default"

that means: "Default.aspx" page Inherits from "_Default.cs" C# Class

But how it's possible ?! Default.aspx it's a page that contain HTML & Script CODE!

How Asp.Net Do to Transform Default.aspx page to Default_aspx C# Class ?

I will show you ;)

Practice

+Create a simple web Project that contains one page "Default.aspx" :



+Run Web project (F5).

+Go to:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root


you will find 2 directories, select a second one:


this Directory contains all Temporary files that asp.net Generate when Compiling Asp.net application!

we will focus on 2 C# class files:

  • App_Web_qtunxhs-.1.cs: The Factory Class of Default.apsx page
  • App_Web_qtunxhs-.0.cs (is the Main Part of this Article) : it represents the C# class generated from HTML&Script code on Default.aspx page, Open it on Visual Studio you will Find:
  1. Class Definition: As you see the Generated Class default_aspx inherits(1) from AspxToCsharp._Default
    public class default_aspx : global::AspxToCsharp._Default, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {

    private static bool @__initialized;

    private static object @__fileDependencies;
  2. Constructor: set dependecies with Default.aspx page
  3. [System.Diagnostics.DebuggerNonUserCodeAttribute()]public default_aspx() {
    string[] dependencies;
    ((global::AspxToCsharp._Default)(this)).AppRelativeVirtualPath = "~/Default.aspx";
    if ((global::ASP.default_aspx.@__initialized == false)) {
    dependencies = new string[1];
    dependencies[0] = "~/Default.aspx";
    global::ASP.default_aspx.@__fileDependencies = this.GetWrappedFileDependencies(dependencies);
    global::ASP.default_aspx.@__initialized = true;
    }
    this.Server.ScriptTimeout = 30000000;
    }
  4. UI Methods (That Generate HTML Controls on Page):
  • Title
  1. HTML:
<title>Aspx To Csharp</title>

  1. C#:

  • Head
  1. HTML :

    <head runat="server">
    <title>Aspx To Csharp</title>
    <script type="text/javascript" >
    function toto()
    {
    alert('toto');
    }
    </script>
    </head>

  2. C#:

  • Input Button:
  1. HTML:
    <input id="Button2" type="button" value="button" onclick="Toto();"/>

  2. C#:

  • Asp.Net Button:
  1. HTML:
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

  2. C#:

  • Form:
  1. HTML:
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    <input id="Button2" type="button" value="button" onclick="Toto();"/>
    </form>
  2. C#:


  • Page(Default.aspx):
  1. HTML:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Aspx To Csharp</title>
    <script type="text/javascript" >
    function toto()
    {
    alert('toto');
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    <input id="Button2" type="button" value="button" onclick="Toto();"/>
    </form>
    </body>
    </html>

  2. C#:


Now you know How Asp.Net Transform "Default.apsx" page into C# Class "Default_aspx" for Comple it with is Code behind class "_Default" to generate WebApplication DLL.

Conclusion:
P.S: The WebApplication DLL (AspxToCsharp.dll) is instantiated for each client request!

2 commentaires:

  1. As Far As I know this is the model of Asp.net 2.0 [and +] Website projects.
    For Web Application Project the compilation is a bit different

    RépondreSupprimer

Rechercher dans ce blog