9th June - ASP - Net Architecture
9th June - ASP - Net Architecture
net Architecture
Jeremy Boyd, Senior Technical Lead - Intergen MSDN Regional Director New Zealand
Objectives
Discuss history of ASP.NET
Dynamic web content ASP
ASP
Active Server Pages (ASP) simplify common tasks ISAPI DLLs were being used for
ASP was introduced because web developers were building ISAPI extension DLLs to perform things like database queries and posting back HTML Each different type of request required a new ISAPI extension DLL to be written ASP.DLL is a generic ISAPI DLL that reads .ASP files, parses and executes server side script blocks, and serves up result
ASP.NET == ASP.NExTversion
On one hand, ASP.NET is an evolution of the ASP and is just the next version of ASP
Same intrinsic objects available Script and html can be mixed Some ASP code can be ported with no changes Server-side Javascript is still supported
Fundamental change
ASP.NET is more than just the next version of ASP
Pages are compiled into assemblies improving performance and diagnostics Code-behind encourages better separation of code from HTML Extensible, server-side control architecture Server-side data binding model Form validation architecture Web services allow assemblies to expose themselves as SOAP servers
What is ASP.NET?
At a high level, ASP.NET is a collection of .NET classes that collaborate to process an HTTP request and generate an HTTP response
Some classes are loaded from system assemblies Some classes are loaded from GAC assemblies Some classes are loaded from local assemblies To work with ASP.NET, you must build your own classes that integrate into its existing class structure Some of your classes will be in pre-built assemblies Some of your classes will be in assemblies
System Assemblies
system.web. dll system.data. dll mscorsvr.dll
GAC Assemblies
bargraph.dll mygacutil.dll acmeutil.dll
Local Assemblies
mypage.dll 32wie4kg.dll myctrl.dll
Pipeline architecture
ASP.NET uses the CLR to replace IIS's ISAPI/ASP architecture
User-defined handler objects used to dispatch HTTP requests Requests dispatched through ASP.NET-provided ISAPI extension (aspnet_isapi.dll)
Handlers run in an ASP.NET-provided worker process (aspnet_wp.exe in IIS 5, w3wp.exe in IIS 6) Many IIS features bypassed in favor of ASP.NETprovided features (WAM-based process isolation, ASP object model, and session management)
IHttpHandler
handler
kernel
http.sys
GET /foo/foo.aspx
Page compilation
Every ASP.NET page is compiled into an assembly on first access
The generated assembly contains a single class that derives from System.Web.UI.Page The generated Page-derived class is the file name of the page, replacing the "." with a "_" (like foo_aspx) The generated assembly is stored in the 'Temporary ASP.NET Files' directory on the server machine
Locate foo.aspx Generate Page-derived class foo_aspx from file Compile to assembly no
yes
HTTP Response HTTP/1.1 200 OK ... Content-Type: text/html; Content-Length: 300 <html><body> ... </body></html>
ASP.NET basics
Each ASP.NET page is parsed and compiled into a class that extends System.Web.UI.Page
Page class implements IHttpHandler A lot of the Page class is dedicated to forms/control processing Exposes HttpContext properties as own properties
System.Web.UI.Page
The Page class provides facilities for rendering HTML
Response and Request objects are available as properties of the class Methods for rendering are provided Events associated with generating the page are defined
System.Web.UI.Page
class Page : TemplateControl, IHttpHandler { // State management public HttpApplicationState Application {get;} public HttpSessionState Session {virtual get;} public Cache Cache {get;} // Intrinsics public HttpRequest Request {get;} public HttpResponse Response {get;} public HttpServerUtility Server {get;} public string MapPath(string virtualPath);
// Client information public string ClientTarget {get; set;} public IPrincipal User {get;} //...
}
System.Web.UI.Page
class Page : TemplateControl, IHttpHandler { // Core public UserControl LoadControl(string virtualPath); public virtual ControlCollection Controls {get;} public override string ID { get; set;}
public bool IsPostBack {get;} protected virtual void RenderControl(HtmlTextWriter writer); // Events public event EventHandler Init; public event EventHandler Load; public event EventHandler PreRender; public event EventHandler Unload;
//...
}
Class creation
Classes created from .aspx files can be customized
Server-side script blocks are added to the class definition
Member variables Member functions
Aspx == Class
member variable declaration member function declaration <%@ <%@Page PageLanguage="C#" Language="C#"%> %> <html><body> <script language="C#" runat="server"> private ArrayList _values = new ArrayList(); private void PopulateArray() { _values.Add("v1"); _values.Add("v2"); _values.Add("v3"); _values.Add("v4"); } </script> <h2>aspx==class!</h2> <ul> <% <% PopulateArray(); for (int i=0; i<_values.Count; i++) Response.Output.Write("<li>{0}</li>", _values[i]); %> %> </ul> </body> </html>
} //...
Code behind
In addition to customizing the generated Page class using embedded code, ASP.NET supports page inheritance
Technique of Page inheritance is called code-behind Supported through the Inherits attribute of the Page directive Promotes separation of code and presentation Code-behind files can be pre-compiled and placed in a directory named /bin at the top level of the application
Code-behind files can be compiled on demand using the src attribute of the Page directive
SamplePage.cs
namespace EssentialAspDotNet.Architecture { public class SamplePage : Page { private ArrayList _values = new ArrayList(); public SamplePage() { _values.Add("v1"); _values.Add("v2"); _values.Add("v3"); _values.Add("v4"); } protected void WriteArray() { for (int i=0; i<_values.Count; i++) Response.Output.Write("<li>{0}</li>", _values[i]); } } }
ASP.CodeBehind_aspx
<%@ Page Page Language="C#" Language="C#" src="SampleCodeBehind.cs" src="SampleCodeBehind.cs" <%@ Inherits="EssentialAspDotNet.Architecture.SamplePage"%> Inherits="EssentialAspDotNet.Architecture.SamplePage"%> <html><body> <h2>aspx==class!</h2> <ul> <% <% WriteArray(); %> %> </ul> </body> </html>
Shadow Copying
All assemblies in the /bin directory are shadow copied
Placing assemblies in /bin makes them available to all pages in that application These assemblies are not referenced directly by ASP.NET Instead they are copied to an obscure location prior to loading If the original file ever changes, the file is re-copied Enables xcopy deployment
ShadowCopyDirectories
Fusionmanaged directories
ASP->ASP.NET Migration
Several options for migrating 'classic' ASP applications
Run ASP side-by-side with ASP.NET, developing new pages/apps in ASP.NET
Quickest path, but session and application state is not shared
Convert existing ASP pages to ASP.NET (but keep using old libraries - ADO and msxml)
Access to COM libraries incurs interop boundary crossing
Convert existing ASP pages to ASP.NET with new libraries (ADO.NET and System.XML)
Summary
ASP.NET is an evolution of dynamic web page generation techniques All pages in ASP.NET are compiled assemblies Code-behind is a useful technique for separating code logic from presentation Shadow copying enables 'xcopy' deployment Migrating ASP applications typically requires some explicit conversion on your