-----------------------------------------------------------

New Message on MumbaiUserGroup

-----------------------------------------------------------
From: Swapnil_B1
Message 1 in Discussion

  
Folks,  
                     In my previous articles on ASP.NET we have gone through 
the ASP.NET Coding Model, Application Directories and Provider Model. We have 
seen various providers and creating the custom providers etc. In this article 
we will compare the compilation model of ASP.Net 1.X and ASP.NET 2.0. We will 
also look at some of the new features in ASP.NET 2.0 compilation model.  
ASP.NET 1.x Compilation Model  
ASP.NET supports a code behind model, meaning ASPX page can have code behind 
associated with it. There is a complex relationship between the aspx page and 
code behind page. The code behind page is derived from the System.Web.UI.Page 
class and aspx is inherited from code behind. The following is the page 
directive referring to Code behind  
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="SampleWeb.WebForm1" %>  
The Visual Studio Web form designer uses the code-behind attribute to refer to 
the code-behind page and tells the designer where to find the file. Visual 
Studio pre-compiles all the code-behind pages into a project assembly and 
places it in the bin directory. Because my Web project is named SampleWeb, the 
DLL is named SampleWeb.DLL. The Inherits attribute tells the compiler the class 
in the project assembly from which the ASPX page inherits. In the previous code 
snippet, it tells the compiler to look for a class called WebForm1 in the 
SampleWeb.DLL. If we want each page as a separate assembly (instead of the 
code-behind pages being precompiled into a project file), the framework 
supports a slightly different model for ASP.NET. In this case there is no 
code-behind attribute in the page directive. The link between the ASPX file and 
its corresponding code-behind page is managed by the Src attribute. The Src 
attribute points to the code-behind page as shown here. The code runs exactly 
the same way in both the models:  
<%@ Page language="c#" Src="WebForm2.cs" AutoEventWireup="false" 
Inherits="WebForm2 " %>  
ASP.NET compiles the ASPX page the first time the user requests it and then 
stores it into its cache. Next time around ASP.NET checks the cache to see if 
the copy of the page exists. In case it cannot find one, it creates a temporary 
source code file that represents the Page class. It puts it in the temporary 
ASP.NET files folder that exists inside the 
Windows\Microsoft.Net\Framework\Version\TemporaryASP.NETFiles\<Website> folder. 
Let's look at a simple example. Code snippet 1 shows the ASPX page 
WebForm1.aspx. Code snippet 2 shows the code-behind page that is associated 
with the ASPX file. On compilation, you will notice a class file in this 
temporary folder that has a class that is named WebForm1 _aspx just like the 
ASPX page. However, the dot has been replaced by an underscore. The 
auto-generated class inherits from the code-behind class as instructed, which 
in our example is SampleWeb. WebForm1.  
There are a lot of confusing things with the current way of doing things. There 
is a complex division of responsibility between the base class (i.e., the 
code-behind page) and the ASPX page. This has been simplified in ASP.NET 2.0 
Compilation Model  
ASP.NET 2.0 Compilation Model
The first thing that needs to be pointed out about the ASP.Net 2.0 model is 
that the complex division of responsibility between the code-behind and the 
ASPX page is removed. Code Snippet 3 and 4 show the ASPX page and the 
code-behind file. Notice that the code-behind file is much cleaner than the 
ones in ASP.NET 1.X. The code-behind page just contains the user-defined code. 
It no longer contains all the control declarations that are needed to 
synchronize the ASPX file and the code-behind page in ASP.NET 1.X. In ASP.NET 
2.0, the association between the ASPX and the code-behind page is specified 
with a new directive called Compilewith shown in the following code snippet:  
<%@ Page Language="C#" CompileWith="Default.aspx.cs" ClassName="Default_aspx" 
%>  
Also, notice a new directive called ClassName, which in this case is 
Default_aspx. This is the name of the class that the page would be given when 
it is compiled. Another interesting point is that the class name of the 
code-behind page is the same as that of the ASPX page. So the obvious question 
is how is it possible to have the same class declaration in two different 
files? This is made possible by the new construct partial class in .NET 
Framework 2.0 that allows a single class to exist in multiple files. In this 
case, when an ASPX page with a new code-behind file is requested, the ASP.NET 
2.0 runtime will actually combine the class definition that is spread in the 
ASPX page and the code-behind page into a single class named Default_aspx. 
Another change that I would like to point out is that the code-behind page no 
longer inherits from System.UI.web.Page and it also no longer needs to define 
all the controls that are defined in the ASPX page. This is because now we are 
dealing with one class, unlike before where we had two different classes with 
one inheriting from the other. ASP.NET 2.0.Visual Studio 2005 no longer 
pre-compiles all the code-behind pages into the project assembly. Because this 
intermediate step of pre-compilation is avoided, there is no need to rebuild 
the entire project for a single change. Another big advantage is that Visual 
Studio 2005 IDE now provides the same level of support Intellisense to a 
developer who chooses to follow the single file model for development.  
New Compilation Features
In ASP.NET 1.X, the two modes of compilation available were either compilation 
on first request of each page or batch compilation where in many temporary ASPX 
pages were compiled into a single assembly. Batch compilation is preferred in 
some scenarios to reduce the delay on the first page request. Listing 5 shows 
how to set up batch compilation in ASP.NET 1.X via the Web.config file. ASP.NET 
2.0 has simplified the batch compilation process and all that is needed is a 
single URL request:  
http://localhost/myapp/precompile.axd  
This mode of compilation is known as in-place compilation. The advantage of 
using in-place compilation is that it helps eliminate the time delay in batch 
compilation on the first request. It also helps to identify the compilation 
errors before the users find them. ASP.NET 2.0 supports yet another way for 
compiling and deploying Web applications: full deployment pre-compilation. All 
the code-behind, ASPX, and HTML files are compiled into one or more assemblies 
or executables. This type of compilation helps to protect the intellectual 
property in the code because the ASPX and code-behind files no longer have to 
be deployed as such on the production server. The compiled executable can then 
be copied to its destination via Xcopy or FTP. This model of compilation 
provides the greatest performance and security. However, it comes at a cost 
because we will not be able to modify the Web site once it's deployed. For full 
deployment pre-compilation, we use a command line tool called aspnet_compiler. 
The result of pre-compiling is a site with a bin directory that contains 
assemblies and a number of stub files with the same name as the original pages. 
However, when you open them you will notice that the files contain no code or 
HTML markup inside, although the user will not notice any difference when 
browsing the Web site.  
Swapnil (Swaps)  
http://swapsnet.spaces.live.com/

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/MumbaiUserGroup/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member 
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you 
received this message by mistake, please click the "Remove" link below. On the 
pre-addressed e-mail message that opens, simply click "Send". Your e-mail 
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to