Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
142
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Chapter 5: Building Web Applications

help you establish the value of a property. In Figure 5.14, the properties of the TextBox are displayed in the Properties window, and the BackColor property is being altered.

Figure 5.14. Setting a color using the Properties window

By default, the control’s properties are listed by category, but you can order them alphabetically by clicking the A-Z button. Other buttons in the window allow you to switch between Properties View and Events View.

Executing your Project

You already know from our work back in Chapter 1 that, in order to execute an ASP.NET application, you need to run it through a web server such as IIS. Unfortunately, users of Windows XP Home Edition can’t use IIS, so until now, you’ve most likely been using Cassini, a lightweight web server that Microsoft has made available for development purposes. Here, we’re going to take a look at another option—Visual Web Developer’s built-in web server. We’ll also see how you can use Visual Web Developer to execute your project through IIS.

156

Using Visual Web Developer’s Built-in Web Server

Using Visual Web Developer’s Built-in Web Server

As you already know, Visual Web Developer has an integrated web server, which makes it easy to develop ASP.NET web sites even on machines whose operating systems don’t have IIS—Windows XP Home Edition, for example.

To test the web server, we’ll use a simplified version of the Hello.aspx file we first saw back in Chapter 2, only this time we’ll name it Default.aspx.

Visual Basic File: Default.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>Hello, World!</title> <script runat="server">

Sub Page_Load()

messageLabel.Text = "Hello, World!"

End

Sub

</script>

</head>

 

<body>

 

<form

id="form1" runat="server">

<asp:Label id="messageLabel" runat="server" />

</form>

</body>

 

</html>

 

 

 

C#

File: Default.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>Hello, World!</title> <script runat="server">

void Page_Load()

{

messageLabel.Text = "Hello, World!";

}

</script>

</head>

<body>

<form runat="server">

157

Chapter 5: Building Web Applications

<asp:Label id="messageLabel" runat="server" /> </form>

</body>

</html>

You can start the web server by executing the project: select Debug > Start Without Debugging, or use the keyboard shortcut Ctrl-F5. Visual Web Developer will load Hello.aspx in your system’s default web browser, as shown in Figure 5.15.

Figure 5.15. Executing a page using Visual Web Developer

We executed this project without debugging it, but generally you’ll want to run projects with debugging enabled (using F5, or Debug > Start Debugging). The debugging features enable you to find and fix errors in your code; we’ll learn more about them towards the end of this chapter.

To enable debugging, you must modify an option in a file called Web.config, which is your web application’s configuration file. We’ll take a more in-depth look at Web.config shortly. For now, all you need to know is that the first time you try to debug the project, Visual Web Developer will ask you whether you want to enable debugging in Web.config using a dialog like the one in Figure 5.16.

Click OK to confirm the action.

Once you click OK, the application will execute. The resulting browser window should be the same as the one we saw in Figure 5.15, but this time you have more control over your application, as we’ll soon see.

You can tell that your application doesn’t run through your local IIS if you look at the URL loaded in the browser window. When you execute a project using the integrated web server, it will run on a random port, and the complete URL location will reflect the physical path. In Figure 5.15, you can see that the integrated web server was running on port 1855, which explains the complete URL: http://loc-

158

Using Visual Web Developer’s Built-in Web Server

Figure 5.16. Enabling Debug Mode in Visual Web Developer

alhost:1855/Dorknozzle/Default.aspx. IIS usually runs on the default port, which explains why it doesn’t need to be mentioned explicitly.

Visual Web Developer’s web server displays one small icon in the system tray for each web server instance that’s running. If you run multiple projects at the same time, more web servers will be created and more icons will appear. If you double-click on one of those icons, you’ll be presented with a dialog that contains the web server details, and looks very much like the window shown in Figure 5.17.

Figure 5.17. Visual Web Developer’s web server in action

As it executes the project, Visual Web Developer will launch your system’s default web browser, but you can make it use another browser if you wish. For the purpose of running and debugging your ASP.NET web applications, you might find it easier to use Internet Explorer as your default browser, as it works a little better with Visual Web Developer than do other browsers. For example, if you close the Internet Explorer window while your project runs in debug mode, Visual Web

159