· pluralsight

[Pluralsight – ASP.NET 3.5 Fundamentals] ASP.NET Architecture (Part 1)

1. ASP.NET Architecture

  • Build completely on top of .NET
    • All pages (.aspx) are parsed into classes and compiled into assemblies
    • Assemblies are loaded on-demand into the worker process (w3wp.exe)
    • Classes are created on demand to service requests
  • You work with ASP.NET by
    • Building additional classes
    • Extending base classes
    • Controlling class generation from .aspx files

Everytime an aspx file is requested, it does go through a class creation mechanism, and the first time it’s requested it go through the parsing of the file and the source code generation for that class.


<%@ Page Language="C#" Debug="true" %>
<h1>Hello ASP.NET!</h1>
<h3><%= GetType().Assembly.Location %></h3>

First line is Page directive which can influence the page class generation. Debug = true will make the source code file stay on disk. <%= … %> for server side script. %= is called evaluation syntax.

The line GetType().Assembly.Location will give us the location where classes are generated and we can dig in and see how the source code is generated.

2. Dynamic content

  • ASP.NET inherit a lot of dynamic content syntax
  • Interspersed server-side script <%
  • Server-side evaluation syntax <%=
  • Server-side script block (We can’t but executable code here Ex: Response.Write(“”) since the whole script block will be generated directly in class definition)

3. Server-side control

  • Elements marked as runat=”server” (could be HTML control or server-side control definition)
    • Clean alternative way to interspersed server-side script
    • Added to class definition as member variables
    • Programmatically intereact with object model of controls
    • State retained implicitly across POST request to same page

4. Data binding

  • List controls all support data binding
    • Bind a collection (IEnumerable) to a control
    • Implicit creation of child elements based on collection
protected override void OnLoad(EventArgs e)
_displayItems.DataSource = _displayItemData;


<%@ Page Language="C#" Debug="false" %>

string[] _displayItemData = {"Item #1","Item #2","Item #3","Item #4","Item #5"};
const int _itemCount = 10;
//Response.Write(""); --invalid
string GetDisplayItem(int n){
	return "Item #" + n.ToString();

protected override void OnLoad(EventArgs e)
	_displayList.DataSource = _displayItemData;

<html xmlns="">
<head><title>Simple Page</title></head>
<form runat="server">
<h1>Hello ASP.NET !</h1>
<h3>I was loaded from
<%= GetType().Assembly.Location %>
	<asp:BulletedList runat="server" ID="_displayList">
		<asp:ListItem>Sample Item 1</asp:ListItem>
		<asp:ListItem>Sample Item 2</asp:ListItem>
		<asp:ListItem>Sample Item 3</asp:ListItem>
	<%Response.Write("<h2>Total number of items = "
	+ _itemCount.ToString() + "</h2>");

5. Declarative Data Binding

Version 2.0 ASP.NET introduces data source controls that enables declarative data binding.

In ASP.NET, we decorate code behind class with partial, when we request to an aspx file, that file will be generated to a class which is also a partial class of code behind file. All controls in aspx will be declared as protected variables in generated class.

6. Page Life Cycle

List of events/functions: (common one in bold)

  • PreInit (only place that we can manipulate master pages and themes)
  • Themes initialized, master pages applied, control skins applied
  • Init (at this point, controls have already been constructed in control tree and we can inspect the control tree)
  • InitComplete
  • CreateChildControls (if IsPostBack) | Control/View state loaded (if IsPostBack)
  • PreLoad (do something before all Load logic is executed. Ex: something prior to data binding)
  • Load
  • Server-side events fired (if IsPostBack)
  • LoadComplete (happens right after all server0side events fired which we can add some custom code)
  • CreateChildControls (if !IsPostBack)
  • PreRender (back stop if you need to do something just before rendering actually takes place and the response is sent back to the client )
  • PreRenderComplete
  • Control/View state saved
  • SaveStateComplete
  • Render
  • Unload

7. Event handling

<%@ Page Language="C#" 
    Inherits="DemoWebForm.Default" %>

AutoEventWireup set to true so instead of wiring up event ourself, we can declare some special events and page will automatically subscribe to it. Example: Instead of overriding OnLoad event, we can set AutoEventWireup = “true” and use Page_Load.

Some special ASP.NET directories

8. Shadow copying (a feature of .NET runtime)

  • Assemblies (libraries) placed in /bin are
    • implicitly referenced by every compiled page
    • shadow copied to an alternate location to avoid file locks
    • monitored for changes to shadowed copies can be updated

This is an important feature when you deploy a new update to your website in order to keep the site running. When you are loading an assembly, instead of loading the assembly directly from the location it’s deployed (/bin), .NET will implicitly copy that to another location. And it will reference that one instead. Also, it will add a timestamp to the original one, then if there is a new update on the original one, it will make another copy and refer to that copy instead.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s