15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1152 ❘ ChaPTer 40 cOre Asp.net<br />

It may seem strange that the facility to add JavaScript code to your pages is included with ASP.<strong>NET</strong>.<br />

However, JavaScript allows you to add dynamic client-side behavior to your web pages <strong>and</strong> can be very<br />

useful. This is especially true for Ajax programming, as you will see in Chapter 41, “ASP.<strong>NET</strong> Features.”<br />

It is possible to create ASP.<strong>NET</strong> files in Visual Studio, which is great for you, as you are already familiar<br />

with this environment for <strong>C#</strong> programming. However, the default project setup for web applications in this<br />

environment has a slightly more complex structure than a single .aspx file. This isn’t a problem for you,<br />

however, <strong>and</strong> does make things a bit more logical (more programmer-like <strong>and</strong> less web developer–like).<br />

For this reason, you will use Visual Studio throughout this chapter for your ASP.<strong>NET</strong> programming<br />

(instead of Notepad).<br />

The .aspx files can also include code in blocks enclosed by tags. However, function definitions<br />

<strong>and</strong> variable declarations cannot go here. Instead, you can insert code that is executed as soon as the block<br />

is reached, which is useful when outputting simple HTML content. This behavior is similar to that of<br />

old-style ASP pages, with one important difference — the code is compiled, not interpreted. This results in<br />

far better performance.<br />

Now it’s time for an example. In Visual Studio, you create a new web site by using the File ➪ New ➪ Web<br />

Site . . . menu option or a web application through the File ➪ New ➪ Project . . . menu option by selecting<br />

a web application template. In either case, from the dialog box that appears, select the Visual <strong>C#</strong> language<br />

type <strong>and</strong> the specific template you want. For web sites, you have an additional choice to make. Visual Studio<br />

can create web sites in a number of different locations:<br />

➤<br />

➤<br />

➤<br />

➤<br />

On your local IIS web server<br />

On your local disk, configured to use the built-in Visual Web Developer web server<br />

At any location accessible via FTP<br />

On a remote web server that supports Front Page Server Extensions<br />

The latter two choices use remote servers so you are left with the first two choices. In general, IIS is the best<br />

place to install ASP.<strong>NET</strong> web sites because it is likely to be closest to the configuration required when you<br />

deploy a web site. The alternative, using the built-in web server, is fine for testing but has certain limitations:<br />

➤<br />

➤<br />

➤<br />

Only the local computer can see the web site.<br />

Access to services such as SMTP is restricted.<br />

The security model is different from IIS — the application runs in the context of the current user<br />

rather than in an ASP.<strong>NET</strong>-specific account.<br />

This last point requires clarification because security is very important when it comes to accessing databases<br />

or anything else that requires authentication. By default, web applications running on IIS do so in an<br />

account called ASP<strong>NET</strong> on IIS5 web servers, or in an account called NT AUTHORITY\<strong>NET</strong>WORK<br />

SERVICE on IIS6 <strong>and</strong> above. This is configurable if you are using IIS, but not if you use the built-in<br />

web server.<br />

For the purposes of illustration, however, <strong>and</strong> because you may not have IIS installed on your computer,<br />

you can use the built-in web server. You aren’t worried about security at this stage, so you can go with<br />

simplicity.<br />

Web applications don’t prompt you to choose a location like web sites do. Instead, web sites<br />

must be deployed to web servers in a separate step — although for testing they will use the VWD<br />

web server.<br />

Create a new ASP.<strong>NET</strong> web site called PCSWebSite1 using the File System option <strong>and</strong> the Empty Web Site<br />

template, at C:\ProCSharp\Chapter40, as shown in Figure 40-2.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!