NET
Objectives
Real-world applications are typically multi-tier, distributed designs involving many components the web server being perhaps the most important component in today's applications...
Microsoft
Part 1
Web-based applications
Microsoft
Application design
Many applications are designed with N levels or "tiers" good separation of concerns enables reuse of back-end tiers across varying FEs
object
object
Front-end
object
Presentation
Business
Data Access
Data
Microsoft
Web-based applications
Web-based apps offer many advantages: extend reach of application to people AND platform based on open, non-proprietary technologies Two types: WebForms: GUI-based app displayed in browser WebServices: object-based app returning raw XML
HTTP, HTML Web Page
browser
obj obj
obj
custom FE
SOAP, XML
obj
Web server
other App
Microsoft
Part 2
IIS
Microsoft
. . .
Microsoft
Windows Server
Configuring IIS
Configured manually via: control panel, admin tools, Internet Information Services
Microsoft
A web site
IIS deals with web sites A web site = a web application
How IIS works: each web site has its own physical directory on hard disk each web site is assigned a virtual name for that directory users surf to web site based on virtual name Example: web site lives in C:\Inetpub\wwwroot\WebSite
web site's virtual name is "AAAPainting" IIS maps virtual to physical
Microsoft
Microsoft
10
Microsoft
11
<html> <h3>Welcome <HR> <ol> <li> <A <li> <A <li> <A </ol> <HR> </html>
Microsoft
to AAA Painting</h3>
12
HTML
Static web sites are typically pure HTML pages may also contain script code that runs on client-side
<title>Welcome to AAA Painting</title> <html> <h3>Welcome <HR> <ol> <li> <A <li> <A <li> <A </ol> <HR> </html>
to AAA Painting</h3>
Microsoft
13
Microsoft
14
ASP
Active Server Pages Microsoft's server-side programming technology ASP based on scripting languages, interpreted ASP.NET based on .NET languages, compiled, faster,
http://host/page.asp
client
HTML
ASP engine
IIS
Microsoft
15
Example
We want to dynamically create web page of attendee's i.e. generate the page by reading names from a database
ASP Page
IIS
Microsoft
16
default.asp
dbConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" + _ "Data Source=C:\Inetpub\wwwroot\Workshop\Attendees.mdb") rs.ActiveConnection = dbConn : rs.Source = sql : rs.Open Do While Not rs.EOF firstName = rs("FirstName") lastName = rs("LastName") Response.Write("<li> " + firstName + " " + lastName) rs.MoveNext() Loop rs.Close : dbConn.Close End Sub </SCRIPT>
Microsoft
18
Microsoft
19
Part 3
WebForms
Microsoft
20
Microsoft
21
WebForms
Web-based, form-based .NET apps
Based on many technologies: IIS ASP.NET (extension to IIS) .NET Framework SDK (CLR, FCL, tools) Visual Studio .NET (drag-and-drop creation)
Microsoft
22
Abstraction
Like WinForms, WebForms are based on classes in FCL separates WebForm app from underlying platform
object
System.Web.UI.Page
CLR
Windows OS
Microsoft
23
Microsoft
24
Example
A simple WebForms app to view attendee info from DB
Microsoft
25
Microsoft
26
Microsoft
27
Microsoft
28
Web controls: work like WinForm counterparts HTML controls: mimic standard HTML controls
Microsoft
29
Microsoft
30
(5) Run!
You can run from within VS You can debug from within VS
How does it work? starts up a session of IE attaches debugger to IIS displays .aspx page marked as "Start Page" in your project right-click on .aspx page you want to start with select "Set as Start Page"
Microsoft
31
Microsoft
32
Microsoft
33
Solutions: if you want to tell user something, display via label on page if you want to show another page, redirect browser
Microsoft
34
Web-based dialogs
To display a message to user:
private void cmdViewInfo_Click(...) { if (this.ListBox1.SelectedItem == null) { // nothing selected this.lblErrorMsg.Text = "Please select an attendee!"; return; } . . .
Microsoft
35
ASP.NET engine
client
IIS
Microsoft 36
#3: AutoPostBack
In fact, some events aren't posted right away instead event is "queued" until page is eventually posted back To force immediate postback? set control's AutoPostBack property (if it has one)
Example: list box doesn't postback when you click on an item instead, event is queued until later (e.g. button is clicked)
Microsoft
37
#4: Postbacks
There is a distinction made between: first request that is posted to server for page X by client C subsequent "postbacks" of page X to client C Example: Page_Load event triggers every time but typically only need to initialize form the first time!
private void Page_Load(...) { if (this.IsPostBack) // no need to reload list box! return;
Microsoft
38
#5: Statelessness
Web apps are stateless Each page request is a self-contained operation: connection is opened request is posted result is returned connection is closed
Implications? By default: no session state (i.e. no data for client C across pages) no global state (i.e. no data across all clients & pages) postback state *is* maintained for you by .NET e.g. contents of list box
Microsoft
39
Summary
Web-based applications are commonplace Web-based applications are often mission-critical Two options: WebForms: form-based WebServices: object-based
Microsoft
40
References
Books: F. Onion, "Essential ASP.NET" D. Sussman et al., "Beginning ASP.NET 1.0 with C#" Web sites: http://www.asp.net/ http://www.asp.net/webmatrix/
Microsoft
41
Lab?
Work on lab #5, "WebForms"
Microsoft
42