ASP.NET

Visual Studio and ASP.NET

Visual Studio .NET 2005 expands your options for locating your Web sites during development. The Visual Studio .NET 2005 wizards define four separate Web site projects: local IIS Web sites, file system Web sites, FTP Web sites, and remote Web sites.

Kinds of Web Sites

Here's a rundown of the different types of Web sites available using the project wizard. Each is useful for a particular scenario, and having these options makes it much easier to develop and deploy an ASP.NET application with Visual Studio 2005 than with earlier versions.

Local IIS Web Sites

Creating a local IIS Web site is much like creating a Web site using the older versions of Visual Studio.NET specifying a local virtual directory. This option creates sites that run using IIS installed on your local computer. Local IIS Web sites store the pages and folders in the IIS default directory structure (that is, \Inetpub\wwwroot). By default, Visual Studio creates a virtual directory under IIS. However, you may create a virtual directory ahead of time and store the code for your Web site in any folder. The virtual directory just needs to point to that location.

One reason to create a local Web site is to test your application against a local version of IIS, for example, if you need to test such features as application pooling, ISAPI filters, or HTTP-based authentication. Even though a site is accessible from other computers, it's often much easier to test these aspects of your application when you can see it interact with IIS on your computer. To create a local Web site, you need to have administrative rights. For most developers, this is not an issue.

File System Web Sites

File system Web sites live in any folder you specify. The folder may be on your local computer or on another computer sharing that folder. File system Web sites do not require IIS running on your computer. Instead, you run pages by using the Visual Studio Web server.

Visual Studio Web Server

One of the issues plaguing Web developers using earlier versions of Visual Studio was the fact that it used IIS to serve up pages. That meant that developers needed to have IIS fully enabled on their machines to be able to develop effectively. This created a possible security compromise. Visual Studio 2005 now includes its own built-in Web server. This lets you develop Web applications effectively even if you don't have IIS installed on your development machine.

File system Web sites are useful for testing your site locally but independently of IIS. The most common approach is to create, develop, and test a file system Web site and then when it is time to expose your site, to simply create an IIS virtual directory and point it to the pages in the file system Web site.

Because file system Web sites employ the Visual Studio Web server rather than IIS, you may develop your Web site on your computer even when logged on as a user without administrative rights.

This scenario is only useful for developing and testing those features of your site that you develop. Because IIS is out of the picture, you won't be able to work with (or have to deal with) such IIS features as ISAPI filters, application pooling, or authentication.

FTP Web Sites

In addition to creating HTTP-based sites, you may use Visual Studio to manage Web sites available through an FTP server. For example, if you use a remote hosting company to host your Web site, an FTP offers a convenient way to move files back and forth between your development location and the hosting location.

Visual Studio connects to any FTP server for which you have reading and writing privileges. You then use Visual Studio to manage the content on the remote FTP server.

You might use this option to test the Web site on the live server where it will actually be deployed.

Remote Web Sites

The final option for developing and managing Web sites through Visual Studio is to use the remote Web sites option. Remote Web sites use IIS on another computer that is accessible over a local area network. Visual Studio 2003 also supported an option very like this one. In addition to running IIS, the remote computer must have IIS installed and needs to have FrontPage 2002 Server Extensions installed. Pages and folders on a remote site become stored under the default IIS folder on the remote computer.

This option is useful if you decide you want to test the Web site on its actual deployment server. In addition, the entire development team can work on the site simultaneously. The downside of this approach is that debugging and configuring a Web site remotely is tricky.

Hello World and Visual Studio

To get started, let's use Visual Studio to generate the HelloWorld Web application.

  1. Create a new Web site.

    To create a new Web site, select the following menu combination: File | New | Web Site. Visual Studio will display a dialog box like this one:

    Graphic

    Give the Web site a useful name like ASPNETStepByStepExamples.

    Notice that several different kinds of sites are showing in the dialog box. Choose Empty Web Site for this example.

    Choosing Empty Web Site causes Visual Studio to generate an ASP.NET solution file within a directory named Visual Studio 2005\Projects in your normal Documents directory. Visual Studio will also create a new directory within your inetpub\wwwroot directory and map it as an IIS virtual directory. However, the virtual directory will be devoid of any files.

    Selecting ASP.NET Web Site causes Visual Studio to generate a directory structure similar to the one generated by ASP.NET Web Site. However, Visual Studio will throw in a default Web form and source code to go with (default .aspx and default.aspx.cs). You'll also get an AppData directory that may contain data pertinent to your site (for example, user names will be contained here).

  2. Choose the language syntax.

    At this point, you have the option of choosing a syntax to use within your code. Choose among Visual Basic, C#, and J#. For this example, choose C#.

  3. Create a local Web site.

    For this example, select HTTP from the location combo box to run this Web site locally on your machine. Visual Studio's default option is to create a Web site on your local machine. Clients trying to access your Web site will have their requests directed through IIS. This is the best option to choose when learning ASP.NET because it gives you the chance to work with your Web site as an entire system, and you can use tracing and debugging on your local machine.

  4. Add a HelloWorld page.

    To add the HelloWorld page to the new site, select Website | Add New Item… to reach the Add New Item dialog box:

    Graphic

    This dialog box lists all the various pieces you may add to your Web site. Topping the list is an item named Web Form. Select this option, and then type Helloworld.aspx into the Name text box. Leave the other defaults the same.

    Visual Studio will immediately confront you with the pure ASP.NET code from the Helloworld.aspx file.

    Notice that the code generated by Visual Studio includes directives near the top connecting Helloworld.aspx to the accompanying source file Helloworld.aspx.cs (with the CodeFile and Inherits directives). Following the directives is some initial HTML produced by Visual Studio.

    Graphic

    At this point, take a moment to explore the layout of Visual Studio. Along the top of the window, you'll see a number of toolbar buttons and menu options. We'll visit most of them throughout the course of this text. Directly beneath the code window, you'll see two tabs labeled Design and Source (the Source tab is selected by default). If you select the Design tab, you'll see what the page will look like in a browser. Right now, the page has no visible tags, so the design view is blank.

    To the right of the Source window, you'll see the Solution Explorer, which lists the components of your application that Visual Studio is currently displaying. Along the top of the Solution Explorer, you'll find a number of buttons. By hovering your cursor over the buttons, you can see what they do. The following figure shows how each button functions.

    Graphic
  5. Write some code into the page.

    Select the View code button from the Solution Explorer. This will show the C# code in the Source code window, like so:

    Graphic

    Add code to show the page's lineage (it's the same code from HelloWorld5 shown above). Add the ShowLineage method to the Helloworld.aspx.cs file.

    Graphic
    public void ShowLineage()
       {
            Response.Write("Check out the family tree: <br> <br>");
            Response.Write(this.GetType().ToString());
            Response.Write(" which derives from: <br> ");
            Response.Write(this.GetType().BaseType.ToString());
            Response.Write(" which derives from: <br> ");
            Response.Write(this.GetType().BaseType.BaseType.ToString());
            Response.Write(" which derives from: <br> ");
            Response.Write(
              this.GetType().BaseType.BaseType.BaseType.ToString());
            Response.Write(" which derives from: <br> ");
            Response.Write(
              this.GetType().BaseType.BaseType.BaseType.BaseType.ToString());
        }
    
  6. Call the ShowLineage method from the ASPX file.

    Select View Designer from the Solution Explorer, and then select the Source tab near the bottom of the screen.

    Graphic

    Insert Hello World in the page. Then insert a set of execution markers (<% and %>) and insert the call like this:

    <h2> Hello World!!!</h2>
    <%
    ShowLineage();
    %>
    
  7. Now build the project and run the Web site from Visual Studio.

    To build the application, select Build | Solution from the main menu. If the source code has any errors, they'll appear in the Errors window in the bottom window.

    To run the application, select Debug | Start Without Debugging. Visual Studio will start up a copy of an Internet browser (Microsoft Internet Explorer by default) and browse the page. You should see a page like this:

    Graphic

    When you run this application, Visual Studio invokes the Visual Studio Web Server. The files are compiled and moved to the temporary ASP.NET directory. Alternatively, you may precompile the site by selecting Build | Publish from the main menu to pre-compile the code. The precompiled code will land in a subdirectory off your project named PrecompiledWeb. We'll look more closely at the various deexecution and deployment options later on.