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.
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.
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.
In order to complete this walkthrough, you will need the following:
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.
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
Open Visual Studio.
In the File menu, click New Web Site.
The New Web Site dialog box is displayed.
Under Installed Templates, click Visual Basic or Visual C# and then select ASP.NET Empty Web Site.
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.
Visual Studio creates a Web site project that includes only a Web.config file.
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
In Solution Explorer, right-click the name of the Web site project and click Add New Item.
Under Installed Templates, select your preferred programming language, and then click Web Form.
In the Name box, type SamplePage.aspx.
Switch to Design view.
From the Standard group in the Toolbox, drag a Label control onto the page.
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
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.
Right-click the App_Code folder, and then click Add New Item.
Under Installed Templates, click the language that you prefer to work with, and then click Class.
In the Name box, type TestClass.
Visual Studio creates a skeleton class file in the programming language that you have specified.
Create a property named TestProperty.
When you are finished, the complete class file will look similar to the following:
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
Open SamplePage.aspx and switch to Design view.
Double-click the Button control to create a Click handler for it.
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:
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
Open the SamplePage.aspx page.
The page appears in the browser.
Click Button and make sure that text appears in the Label control.
Close the browser.
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
On the Build menu, click Publish Web Site.
The Publish Web Site dialog box appears.
In the Target Location box, enter c:\CompiledSite.
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.
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.
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.
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
In Windows Explorer, navigate to the folder that you specified as the target for the Publish Web Site command.
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.
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.
You can now test the published Web site by running it.
To test the published Web site
Create an IIS virtual directory that points to the target folder.
From the Windows Start menu, select Run, enter inetmgr, and click OK.
The Internet Information Services (IIS) Manager dialog box appears.
In the Connections pane, expand your server name and expand Sites.
Right-click Default Web Site and select Add Virtual Directory.
The Add Virtual Directory dialog box appears.
In the Alias box, enter CompiledWebSite.
In the Physical path box, enter C:\CompiledWebSite.
In the Connections pane of IIS Manager, right-click the new virtual directory and select Convert to Application.
The Add Application dialog box appears.
Open the browser and type the following URL:
The SamplePage.aspx page appears. However, this time you are viewing the version of the page that was created by the precompiler for deployment.
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 Deploying Web Site Projects by Using the Copy Web Site Tool.
Publish the Web site to a remote IIS Web site.