Walkthrough: Publishing a Web Site

Provides step-by-step instructions for using the Publish Web Site utility of the Microsoft Visual Web Developer Web development tool to compile a Web site, and then copy the output to an active Web site.

If you want to deploy a finished Web site to a server, you can use the Publish Web Site utility that is included with the Microsoft Visual Web Developer Web development tool. The Publish Web Site utility precompiles the pages and code that are in the Web site and writes the compiler output to a folder that you specify. You can then copy the output to the target Web server and run the application from there.

Note

The Publish Web Site utility is not available in Visual Web Developer Express edition.

Tasks illustrated in this walkthrough include the following:

  • Using the Publish Web Site utility to create precompiled output.

Prerequisites

In order to complete this walkthrough, you will need the following:

  • Visual Web Developer.

    The Visual Web Developer Express edition does not support the Publish Web Site utility.

  • Access to Microsoft Internet Information Services (IIS) so that you can test the result of publishing a Web site.

    In this walkthrough, it is assumed that you have IIS running on your own computer. Alternatively, you can use any instance of IIS for which you have permission to create a virtual directory.

Creating the Web Site

If you have already created a Web site in Visual Web Developer by completing Walkthrough: Creating a Basic Web Page in Visual Web Developer, you can use that Web site and go to the next section. Otherwise, create a new Web site and page by following these steps.

For this walkthrough, you will create a file system Web site.

To create a file system Web site

  1. Open Visual Web Developer.

  2. On the File menu, click NewWeb Site.

    The New Web Site dialog box appears.

  3. Under Visual Studio installed templates, click ASP.NET Web Site.

  4. In the left-most Location list, click File System.

  5. In the right-most Location list, enter the name of the folder where you want to keep the pages of the Web site.

    For example, type the folder name C:\WebSites.

  6. In the Language list, click the programming language that you prefer to work in.

  7. Click OK.

    Visual Web Developer creates the folder and a new page named Default.aspx.

Creating a Test Page and Class

For this walkthrough, you will create a Web page with some controls. You will also create a class file that you will use in the Web page. Creating both a Web page and a separate class will let you see how the publish process precompiles the contents of the Web site.

You will start by creating a new page, and then adding a button and label to the page.

To create the page and add controls

  1. In Solution Explorer, right-click the name of the Web site and click Add New Item.

  2. Under Visual Studio installed templates, click Web Form.

  3. In the Name box, type SamplePage.aspx.

  4. In the Language list, click the programming language that you prefer to work in.

  5. Click Add.

  6. Switch to Design view.

  7. From the Standard group in the Toolbox,drag a Labelcontrol onto the page.

  8. From the Standard group in the Toolbox**,** drag a Button control onto the page and position it next to the Label control.

Next, you will create the source code for a simple class that has a single property in it. You will use the class in the code for your page.

To create a class

  1. In Solution Explorer, right-click the name of the Web site, point to Add ASP.NET Folder, and then click App_Code.

    A new folder named App_Code appears in your application in Solution Explorer. The App_Code folder is a special reserved ASP.NET application folder. For more information, see ASP.NET Web Site Layout.

  2. Right-click the App_Code folder, and then click Add New Item.

  3. Under Visual Studio installed templates, click Class.

  4. In the Name box, type TestClass.

  5. In the Language list, click the programming language that you prefer to work in.

    Note

    The programming language that you select does not have to be the same as the programming language that you use in the .aspx page.

  6. Click Add.

    Visual Web Developer creates a skeleton class file in the programming language that you have specified. Notice that the extension of the class file name matches the language that you have selected. For example, if you are creating a class in Microsoft Visual Basic, the file name extension is .vb.

  7. Create a property named TestProperty.

    When you are finished, the complete class file will look similar to the following:

    Imports Microsoft.VisualBasic
        Public Class TestClass
        Private TestPropertyValue As String
        Public Property TestProperty() As String
            Get
                Return TestPropertyValue
            End Get
            Set(ByVal value As String)
                TestPropertyValue = value
            End Set
        End Property
    End Class
    
    using System;
    public class TestClass
    {
        public TestClass() { }
        private string TestPropertyValue;
        public string TestProperty
        {
            get{ return TestPropertyValue; }
            set{ TestPropertyValue = value; } 
        }
    }
    

Now, you can use the class in the page. Notice that you do not have to compile the class file before using it.

To use the class in the page code

  1. Open SamplePage.aspx and switch to Design view.

  2. Double-click the Button control to create a Click handler for it.

  3. In the Click handler, create an instance of the TestClass that you created in the preceding procedure, assign a value to the TestProperty property, and then display the TestProperty value in the Label control.

    The complete code will look similar to this:

    Protected Sub Button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs)
        Dim testClass As New TestClass
        testClass.TestProperty = "Hello"
        Label1.Text = testClass.TestProperty
    End Sub
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        TestClass testClass = new TestClass();
        testClass.TestProperty = "Hello";
        Label1.Text = testClass.TestProperty;
    }
    

Testing the Site

Before publishing the site, you can test it to make sure that the site works in the way that you expect.

To test the Web site

  1. Open the SamplePage.aspx page.

  2. Press CTRL+F5.

    The page appears in the browser.

  3. Click Button and make sure that text appears in the Label control.

  4. Close the browser.

Publishing the Web Site

Now that you have a Web site, you can publish it. You can publish the Web site to any location that you have access to on the local computer or on a network that is using any connection protocol that is supported by Visual Web Developer. You have the following options for copying the Web site:

  • Use a UNC share to copy to a shared folder that is on another computer on the network.

  • Use FTP to copy to a server.

  • Use the HTTP protocol to copy to a server that supports FrontPage 2002 Server Extensions from Microsoft.

In this part of the walkthrough, you will publish the Web site to local folder.

To publish the Web site

  1. On the Build menu, click Publish Web Site.

    The Publish Web Site dialog box appears.

  2. In the Target Location box, enter c:\CompiledSite.

    Warning

    All data in the target folder and its subfolders will be deleted. Make sure that you do not type the name of a folder that contains data or contains subfolders with data.

    For the purposes of this walkthrough, you are publishing to a local folder. You could also publish to a UNC share. If you wanted to publish to a remote Web site using HTTP or FTP, the Target Location box is where you would specify the remote server URL.

    The Allow this precompiled site to be updatable option specifies that all program code is compiled into assemblies, but that .aspx files (including single-file ASP.NET Web pages) are copied as-is to the target folder. In this walkthrough, you will not select that option. For detailed information, see Publishing Web Sites.

  3. Click OK.

    Visual Web Developer precompiles the contents of the Web site and writes the output to the folder that you specified. The Output window displays progress messages. If an error occurs during compilation, it is reported in the Output window.

  4. If errors occur during publishing, fix the errors, and then repeat step 1.

Examining the Output of the Publish Web Site Command

It is useful to examine the output of the Publish Web Site command so that you can see what Visual Web Developer has done with your Web site files.

To examine the output of the Publish Web Site command

  1. In Windows Explorer, move to the folder that you specified as the target for the Publish Web Site command.

  2. Using a text editor, such as Notepad, open the SamplePage.aspx file.

    Notice that the file does not contain the markup that you originally had in the file. Instead, the .aspx page is only a placeholder that can be used as part of a URL.

  3. Move to the Bin folder.

    The folder contains two types of files:

    • .compiled files, which correspond to pages.

    • .dll files, which contain the executable code for the Web site, such as the class file that you created.

Remember that the page, its code, and the separate class file that you created have all been compiled into executable code.

Testing the Published Web Site

You can now test the published Web site by running it.

To test the published Web site

  1. Create an IIS virtual directory that points to the target folder.

    You can use the IIS administrative tools or alternatively, use the following steps:

    1. In Windows Explorer, right-click the name of the target folder, and then click Sharing and Security.

    2. On the Web Sharing tab, click Share this Folder.

      The Edit Alias dialog box appears.

    3. If you want, change the name of the alias.

      The default permissions allow Read access and allow Scripts, such as ASP.NET pages, to run.

    4. Click OK to close the Edit Alias dialog box, and then click OK to close the Properties dialog box.

  2. Open the browser and type the following URL:

    https://localhost/CompiledSite/SamplePage.aspx

    The SamplePage.aspx page appears. However, this time you are viewing the version of the page that was created by the precompiler for deployment.

Next Steps

This walkthrough has shown you the basic procedure for publishing a precompiled Web site. Suggestions for more exploration include the following:

  • Experiment with using the Copy Web tool instead, which copies the Web site as-is (as source code) to a target folder.

    For more information, see Copying Web Sites with the Copy Web Site Tool.

  • Publish the Web site to a remote IIS Web site.

See Also

Tasks

Walkthrough: Copying a Web Site Using the Copy Web Site Tool

Concepts

ASP.NET Deployment Overview

Publishing Web Sites