Evjen c01.tex V2 - 01/28/2008 12:27pm Page 2
Chapter 1: Application and Page Frameworks
This built-in Web server was previously presented to developers as a code sample called Cassini. In fact,
the code for this mini Web server is freely downloadable from the ASP.NET team Web site found at
www.asp.net
.
The following section shows you how to use the built-in Web server that comes with Visual Studio 2008.
Built-In Web Server
By default, Visual Studio 2008 builds applications without the use of IIS. You can see this when you select
New ➪ Web Site in the IDE. By default, the location provided for your application is in
C:
\
Users
\
Bill
\
Documents
\
Visual Studio 2008
\
WebSites
if you are using Windows Vista (shown in Figure 1-1). It is not
C:
\
Inetpub
\
wwwroot
\ as it would have been in Visual Studio .NET 2002/2003. By default, any site that
you build and host inside
C:
\
Figure 1-2
IIS
From the Choose Location dialog, you can also change where your application is saved and which type
of Web server your application employs. To use IIS (as you probably did when you used Visual Studio
.NET 2002/2003), select the Local IIS button in the dialog. This changes the results in the text area to show
you a list of all the virtual application roots on your machine. You are required to run Visual Studio as
an administrator user if you want to see your local IIS instance.
To create a new virtual root for your application, highlight Default Web Site. Two accessible buttons
appear at the top of the dialog box (see Figure 1-3). When you look from left to right, the first button in
the upper-right corner of the dialog box is for creating a new Web application — or a virtual root. This
button is shown as a globe inside a box. The second button enables you to create virtual directories for
3
Evjen c01.tex V2 - 01/28/2008 12:27pm Page 4
Chapter 1: Application and Page Frameworks
Figure 1-3
any of the virtual roots you created. The third button is a Delete button, which allows you to delete any
selected virtual directories or virtual roots on the server.
After you have created the virtual directory you want, click the Open button. Visual Studio 2008 then
goes through the standard process to create your application. Now, however, instead of depending on the
built-in Web server from ASP.NET 3.5, your application will use IIS. When you invoke your application,
the URL now consists of something like
http://localhost/MyWeb/Default.aspx
,whichmeansitis
using IIS.
FTP
Not only can you decide on the type of Web server for your Web application when you create it using the
Choose Location dialog, but you can also decide where your application is going to be located. With
the previous options, you built applications that resided on your local server. The FTP option enables
you to actually store and even code your applications while they reside on a server somewhere else in
your enterprise — or on the other side of the planet. You can also use the FTP capabilities to work on
page, whereas the logic piece is stored in a
separate class file:
.aspx.vb
or
.aspx.cs
. It is considered best practice to use the code-behind model
as it provides a clean model in separation of pure UI elements from code that manipulates these elements.
It is also seen as a better means in maintaining code.
One of the major complaints about Visual Studio .NET 2002 and 2003 is that it forced you to use the
code-behind model when developing your ASP.NET pages because it did not understand the code-inline
model. The code-behind model in ASP.NET was introduced as a new way to separate the presentation
code and business logic. Listing 1-1 shows a typical
.aspx
page generated using Visual Studio .NET 2002
or 2003.
6
Evjen c01.tex V2 - 01/28/2008 12:27pm Page 7
Chapter 1: Application and Page Frameworks
Listing 1-1: A typical .aspx page from ASP.NET 1.0/1.1
<
%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
Inherits="WebApplication.WebForm1"%
>
<
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<
>
<
P
>
What is your name?
<
br
>
<
asp:TextBox id="TextBox1" runat="server"
><
/asp:TextBox
><
BR
>
<
asp:Button id="Button1" runat="server" Text="Submit"
><
/asp:Button
><
/P
>
<
P
><
asp:Label id="Label1" runat="server"
><
/asp:Label
><
/P
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
’CODEGEN: This method call is required by the Web Form Designer
’Do not modify it using the code editor.
InitializeComponent()
End Sub
Continued
7
Evjen c01.tex V2 - 01/28/2008 12:27pm Page 8
Chapter 1: Application and Page Frameworks
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
’Put user code to initialize the page here
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Label1.Text = "Hello " & TextBox1.Text
End Sub
End Class
In this code-behind page from ASP.NET 1.0/1.1, you can see that a lot of the code that developers never
have to deal with is hidden in the
#Region
section of the page. Because ASP.NET 3.5 is built on top
of .NET 3.5, which in turn is utilizing the core .NET 2.0 Framework, it can take advantage of the .NET
Framework capability of partial classes. Partial classes enable you to separate your classes into multi-
ple class files, which are then combined into a single class when the application is compiled. Because
ASP.NET 3.5 combines all this page code for you behind the scenes when the application is compiled,
the code-behind files you work with in ASP.NET 3.5 are simpler in appearance and the model is easier
Web Service
.asmx
file
8
Evjen c01.tex V2 - 01/28/2008 12:27pm Page 9
Chapter 1: Application and Page Frameworks
Figure 1-6
By using the Web Form option with a few controls, you get a page that encapsulates not only the presen-
tation logic, but the business logic as well. This is illustrated in Listing 1-3.
Listing 1-3: A simple page that uses the inline coding model
VB
<
%@ Page Language="VB" %
>
<
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
>
<
script runat="server"
>
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Label1.Text = "Hello " & Textbox1.Text
End Sub
<
/script
>
<
html xmlns="http://www.w3.org/1999/xhtml"
><
/asp:Textbox
><
br /
>
<
asp:Button ID="Button1" Runat="server" Text="Submit"
OnClick="Button1_Click" /
>
<
p
><
asp:Label ID="Label1" Runat="server"
><
/asp:Label
><
/p
>
<
/form
>
<
/body
>
<
/html
>
C#
<
%@ Page Language="C#" %
well in showing an example in one listing. Even though the example is using an inline coding style, it is
my recommendation that you move the code to employ the code-behind model.
To create a new page in your ASP.NET solution that uses the code-behind model, select the page type
you want from the New File dialog. To build a page that uses the code-behind model, you first select the
page in the Add New Item dialog and make sure the Place Code in Separate File check box is checked.
The following table shows you the options for pages that use the code-behind model.
10
Evjen c01.tex V2 - 01/28/2008 12:27pm Page 11
Chapter 1: Application and Page Frameworks
File Options Using
Code-Behind File Created
Web Form
.aspx
file
.aspx.vb
or
.aspx.cs
file
AJAX Web Form
.aspx
file
.aspx.vb
or
.aspx.cs
file
Master Page
.master
file
.master.vb
or
<
%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %
>
<
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
>
<
html xmlns="http://www.w3.org/1999/xhtml"
>
<
head runat="server"
>
<
title
>
Simple Page
<
/title
>
<
/head
>
<
body
>
<
form id="form1" runat="server"
>
<
/html
>
C#
<
%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %
>
11