Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How aspx is being compiled?

Tags:

asp.net

How .aspx and aspx.cs file is being compiled? will it be two assemblies or both these files combined one and will be created as one assembly? Does any hierarchy/inheritance it follows? .aspx is being created as an corresponding C# code file (only then we could create an assembly right? correct me if i am wrong!) as one in windows form designer.cs file?

like image 905
deen Avatar asked Mar 25 '12 09:03

deen


1 Answers

It depends on the project type. With ASP .Net you can create two kind of projects: Web Application Project and Web Site Project..

How .aspx and aspx.cs file is being compiled?

With Web Application project you compile the code behind of the aspx files, user controls and other code found in this project into one single dll and deploy it on server.

With Web Site Project you simply copy the source code on the server and ASP .Net will handle the compilation for you. The Web Site Project holds the source code of custom classes in App_Code folder (you'll have to read more about these on that link)

Will it be two assemblies or both these files combined one and will be created as one assembly?

In none of these cases, the code found in the aspx, ascx files is not compiled by you (using Visual Studio, etc). ASP .Net parses these files and creates a dll stored in its temp folder. The "aspx, ascx" dlls (it can be more than a single one though) are different files than the one created by you with Visual Studio (and I believe is different than the one created from App_Code folder, since that code can't access code found in the pages).

Does any hierarchy/inheritance it follows?

Yes. When a page is parsed and compiled, that generated class will inherit the one named at "Inherits" attribute, found at @Page directive.

<%@ Page Language="C#" CodeBehind="Default.aspx.cs" 
         Inherits="WebApplication1._Default" %>

ASP .Net parses Default.aspx file and generates a class which inherits WebApplication1._Default

public class default_aspx : global::WebApplication1._Default
{

}

Giving the fact that the generated class from markup inherits a class written by us (usually the one in the code behind, the aspx.cs file), we're free to use its members or methods.

The following method is a method of the _Default class:

 protected string ToUpper(string source)
        {
            return source.ToUpper();
        }

Then in markup we can call:

<form id="form1" runat="server">
  <%= ToUpper("Microsoft") %>
</form>

We can even write in markup something like:

<% SomeValue = 1; %>
<%= SomeValue %>

Where SomeValue it's at least a protected property of the _Default class.

We're free to declare members and write server code in the markup also:

<head runat="server">

    <script runat="server" language="C#">
        private int someCounter = 10;
    </script>

</head>
<body>
    <% for (var i = 0; i < someCounter; i++)
       { %>
        <p>
            Paragraph number:<%= i %>
       </p>
    <% } %>
</body>
</html>

Here I declare a someCounter field and use it to write 10 paragraphs. Of course this is not the recommended way to do such things. Since someCounter is a member of the generated class, this is not accessible in code behind.

There is another huge (and more real) advantage of this architecture. Suppose some pages in the web site are kind static (about.aspx, privacy.aspx, etc), they use same Master page. The code behind in this pages doesn't change. This means we can create other pages and deploy them without doing another compilation of the code behind (this aspect applies to Web Application Projects). Also, before going live, we might allow only one person to see these pages. So to achieve this we create a PreviewPage class

public PreviewPage: System.Web.Page
{
     public  PreviewPage()
     {
          this.Load += (o, e) => {
               // code here to see if the authenticated user has the right to see the page
              // if not, redirect the user to another page 
          }
     }
}

And change the Inherits value:

<%@ Page Language="C#" Inherits="WebApplication1.PreviewPage" %>

aspx is being created as an corresponding C# code file

The Language attribute in the @Page directive dictates what language is used to compile the aspx/ascx file. So you can actually use VB.Net in the aspx file and to compile the website with C# compiler.

This being another compilation, different than the one Visual Studio does, it is made with different options. This is why in web.config there are options to set the compilationMode to Debug/Release and also to instruct the compiler to use the other available options.

like image 153
Adrian Iftode Avatar answered Sep 21 '22 15:09

Adrian Iftode