Thursday, August 25, 2005

Who process the aspx file ?

A web form has two files one is aspx and the other is .aspx.cs ( or aspx.cs ). When we compile an web Application the .aspx.cs converts into dll, (read MSIL )
After compilation we deploy the web App in the following way , copy the dll and the .aspx file into the target directory ( we copy the web.config , global.asax and the referenced dlls also, but please ignore these part for the time being )

When user request for a aspx page , IIS send the request to ASP.NET, and as the aspx pages are not MSIL, so it can not be processed by CLR, correct ? so , here is the confusion who process the aspx pages ? it's like this , when user hit a aspx page for the first time ASP.NET automatically generates a .NET class file that represents the page and compiles it to a second .dll file. The generated class for the .aspx page inherits from the code-behind class that was compiled into the project .dll file. This single .dll file is run at the server whenever the Web Forms page is requested. At run time, this .dll file processes the incoming request and responds by dynamically creating output and sending it back to the browser or client device.
Those who are familiar with ASP, the ASP.NET page framework model represents something new. The ASP model is one of HTML extended with script code. The ASP page consists of script code, such as (JScript, or VBScript) , coexisting with static HTML within the same file. The ASP parser reads the page, interprets it, and runs only the script code to get output results. ASP then merges the output of the script code with the static HTML output found in the page before sending the output back to the browser or client device. In the ASP.NET Page class model, the entire Web Forms page is, in effect, an executable program that generates output to be sent back to the browser or client device.

1 comment:

Neo said...

My friend Atanu had shoot me 3 questions on this post ... here are the questions
1) Where's the second dll located?
2) I open the aspx file in notepad and add a HTML button and save the page. In the browser one can see the changes without compiling.
3) Similarly I can add ASP controls and still can see it in the browser without page recompilation.
4) What happens when the cs file doesn't exist and aspx file contains all server side codes.

Ok let me go one by one ...
1) ASP.NET runtime converts the aspx page to a '*.cs/vb' file and compiles it into a DLL. This DLL is typically stored in the temporary folder 'C:\WINNT\Microsoft.NET\Framework\v1.1.4322\ Temporary ASP.NET Files'.

2) and 3 ) I don't know the exact mechanism but I think the philosophy is .... ASP.NET engine keeps an eye on the aspx files, so whenever the file changed it gets a notification( the way we do in .NET using the FileSystemWatcher class ), hence next time when ASP.NET engine gets a request for this page it first recompile the page and create a new dll.

4) If we create aspx page which contains the server side code as well, ( although this is not preferred way to do things, since it is same as classic ASP model, which we used to call spaghetti code, extremely limited intellisense, and crappy debugging. ). in that case the dll will inherit from System.Web.UI.Page instead of the codebehind page.