Sdílet prostřednictvím


Walkthrough: Deploying a Web Site Project by Using the Publish Web Site Tool

Provides step-by-step instructions for using the Publish Web Site tool of Visual Studio to compile a Web site project, and then copy the output to a specified location.

If you want to deploy a Web site project to a server, you can use the Publish Web Site tool that is included with Visual Studio. The Publish Web Site tool 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 tool is not available in Visual Web Developer Express.

If you were deploying to production, you might want to prevent the production site from responding to page requests during the deployment process, to help avoid errors that might result when changes are in progress. Also, you might want to make sure that the application domain does not recycle multiple times during deployment. This walkthrough does not cover these tasks. For more information, see How to: Prepare to Deploy a Web Project.

Note

This topic applies only to Web site projects. For information about the difference between Web application projects and Web site projects, see Web Application Projects versus Web Site Projects.

Prerequisites

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

  • Visual Studio.

    Note

    This walkthrough assumes that you selected the Web Development collection of settings when you started Visual Studio the first time. For more information, see How to: Select Web Development Environment Settings.

  • Access to Microsoft Internet Information Services (IIS) so that you can test the result of publishing a Web site project. 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 Project

If you have already created a Web site project in Visual Studio (for example, by completing Walkthrough: Creating a Basic Web Page in Visual Studio), you can use that project and go to the next section. Otherwise, create a new Web site project. For this walkthrough, you will create a file system Web site.

To create a file system Web site

  1. Open Visual Studio.

  2. In the File menu, click New Web Site.

    The New Web Site dialog box is displayed.

  3. Under Installed Templates, click Visual Basic or Visual C# and then select ASP.NET Empty Web Site.

  4. In the Web location box, select File System, and then enter the name of the folder where you want to keep the pages for your Web site.

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

  5. Click OK.

    Visual Studio creates a Web site project that includes only a Web.config file.

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 project and click Add New Item.

  2. Under Installed Templates, select your preferred programming language, and then click Web Form.

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

  4. Click Add.

  5. Switch to Design view.

  6. From the Standard group in the Toolbox, drag a Label control onto the page.

  7. 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 project, 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 reserved ASP.NET application folder. For more information, see ASP.NET Web Project Folder Structure.

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

  3. Under Installed Templates, click the language that you prefer to work with, and then click Class.

  4. In the Name box, type TestClass.

  5. Click Add.

    Visual Studio creates a skeleton class file in the programming language that you have specified.

  6. 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; } 
        }
    }
    

You can now use the class in the page. Notice that you do not have to compile the class before using it.

To use the class in the page

  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 Web 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, using any connection protocol that is supported by Visual Studio. You have the following options for copying the Web site:

  • Copy to a folder on the local computer.

  • 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 has FrontPage Server Extensions (FPSE) installed on it.

In this part of the walkthrough, you will publish the Web site to a 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.

    For the purposes of this walkthrough, you are publishing to a local folder. 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.

  3. Clear Allow this precompiled site to be updatable.

    This option specifies that all program code is compiled into assemblies, but that pages and user controls (.aspx, .ascx, and .master files) are copied as-is to the target folder and can be updated as text files without recompiling the project. In this walkthrough, you will not select that option. For detailed information, see ASP.NET Web Site Project Deployment Overview.

  4. Click OK.

    Visual Studio 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.

  5. If errors occur during publishing, fix the errors, and then repeat this procedure.

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 Studio has done with your Web site files.

To examine the output of the Publish Web Site command

  1. In Windows Explorer, navigate 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.

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.

    1. From the Windows Start menu, select Run, enter inetmgr, and click OK.

      The Internet Information Services (IIS) Manager dialog box appears.

    2. In the Connections pane, expand your server name and expand Sites.

    3. Right-click Default Web Site and select Add Virtual Directory.

      The Add Virtual Directory dialog box appears.

    4. In the Alias box, enter CompiledWebSite.

    5. In the Physical path box, enter C:\CompiledWebSite.

    6. Click OK.

    7. In the Connections pane of IIS Manager, right-click the new virtual directory and select Convert to Application.

      The Add Application dialog box appears.

    8. Click OK.

  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:

See Also

Tasks

How to: Deploy a Web Site Project by Using the Publish Web Site Tool

Concepts

ASP.NET Web Site Project Deployment Overview