Deploying an ASP.NET Web Application to a Hosting Provider using Visual Studio or Visual Web Developer: Deploying a Code-Only Update - 8 of 12

By Tom Dykstra|November 17, 2011

This tutorial is part of a series about how to deploy (publish) an ASP.NET web application project by using the one-click publish feature in Visual Studio 2010 or Visual Web Developer Express 2010. For an introduction to the series, see the first tutorial in the series.

Overview

After the initial deployment, your work of maintaining and developing your web site continues, and before long you will want to deploy an update. This tutorial takes you through the process of deploying an update to your application code that does not involve a database change. (You'll see what's different about deploying a database change in the next tutorial.)

Reminder: If you get an error message or something doesn't work as you go through the tutorial, be sure to check the troubleshooting page.

Making a Code Change

As a simple example of an update to your application, you'll add to the Instructors page a list of courses taught by the selected instructor.

If you run the Instructors page, you will notice that there are Select links in the grid, but they don't do anything other than make the row background turn gray.

Instructors_page

Now you'll add code that runs when the Select link is clicked and displays a list of courses taught by the selected instructor .

In Instructors.aspx, add the following markup immediately after the ErrorMessageLabel Label control:

<h3>Courses Taught</h3>
<asp:ObjectDataSource ID="CoursesObjectDataSource" runat="server" TypeName="ContosoUniversity.BLL.SchoolBL"
    DataObjectTypeName="ContosoUniversity.DAL.Course" SelectMethod="GetCoursesByInstructor">
    <SelectParameters>
        <asp:ControlParameter ControlID="InstructorsGridView" Name="PersonID" PropertyName="SelectedDataKey.Value"
            Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>
<asp:GridView ID="CoursesGridView" runat="server" DataSourceID="CoursesObjectDataSource"
    AllowSorting="True" AutoGenerateColumns="False" SelectedRowStyle-BackColor="LightGray"
    DataKeyNames="CourseID">
    <EmptyDataTemplate>
        <p>No courses found.</p>
    </EmptyDataTemplate>
    <Columns>
        <asp:BoundField DataField="CourseID" HeaderText="ID" ReadOnly="True" SortExpression="CourseID" />
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        <asp:TemplateField HeaderText="Department" SortExpression="DepartmentID">
            <ItemTemplate>
                <asp:Label ID="GridViewDepartmentLabel" runat="server" Text='<%# Eval("Department.Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Run the page and select an instructor. You see a list of courses taught by that instructor.

Instructors_page_with_courses

Deploying the Code Update to the Test Environment

Deploying to the test environment is a simple matter of running one-click publish again.

In the Solution Configurations drop-down box, select the Test build configuration. In the Publish profile drop-down box select Test, and then click Publish Web.

Selecting_Test_configuration

If you have customized Visual Studio and these settings aren't available in your toolbars, select the Active solution configuration in the Configuration Manager dialog box (select Configuration Manager from the Build menu), select the Test profile in the Publish Web dialog box (right-click the ContosoUniversity project in Solution Explorer and select Publish), and click Publish in the Publish Web dialog box.

When you click Publish, Visual Studio deploys the updated application and reports success in the Output window.

Output_window_after_deployment

You can now open a browser and run http://localhost/ContosoUniversity/Instructors.aspx. Click a Select link to verify that the update was successfully deployed.

Instructors_page_with_courses_Test

You would normally also do regression testing at this point (that is, test the rest of the site to make sure that the new change didn't break any existing functionality). But for this tutorial you'll skip that step and proceed to deploy the update to production.

Preventing Redeployment of the Initial Database State to Production

In a real application, users interact with your production site after your initial deployment and the databases are populated with live data. If you don't change deployment settings, when you deploy the update, you'll also redeploy the databases in their initial state, which would wipe out all of the live data. Since your SQL Server Compact databases are files in the App_Data folder, you need to prevent this by changing deployment settings so that files in the App_Data folder aren't deployed.

Open the Project Properties window and click the Package/Publish Web tab. Make sure that the Configuration drop-down box has either Active (Release) or Release selected, select Exclude files from the App_Data folder, and then save and close the page.

Exclude_files_from_the_App_Data_folder

Make the same change for deployment to the Test environment: change Configuration to Test and then select Exclude files from the App_Data folder.

Preventing User Access to the Production Site During Update

The change you're deploying now is a simple change to a single page. But sometimes you'll deploy larger changes, and in that case the site can behave strangely if a user requests a page before deployment is finished. To prevent this, you can use an app_offline.htm file. When you put a file named app_offline.htm in the root folder of your application, IIS automatically displays that file instead of running your application. So to prevent access during deployment, you put app_offline.htm in the root folder, run the deployment process, and then remove app_offline.htm.

In Solution Explorer, right-click the solution (not the project) and select New Solution Folder.

Creating_a_solution_folder

Name the folder SolutionFiles. In the new folder create a new HTML page named app_offline.htm. Replace the existing contents with the following markup:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Contoso University - Under Construction</title>
</head>
<body>
  <h1>Contoso University</h1>
  <h2>Under Construction</h2>
  <p>The Contoso University site is temporarily unavailable while we upgrade it. Please try again later.</p>
</body>
</html>

You can copy the app_offline.htm file to the site using an FTP connection or the File Manager utility in the hosting provider's control panel. For this tutorial you'll use the File Manager.

Open the control panel and select File Manager as you did in the Deploying to the Production Environment tutorial. Select contosouniversity.com and then wwwroot to get to your application's root folder, and then click Upload.

Upload_button_in_File_Manager

In the Upload File dialog box, select the app_offline.htm file and then click Upload.

Upload_dialog_box_in_File_Manager

Browse to your site's URL. You see that the app_offline.htm page is now displayed instead of your home page.

App_offline.htm_page_in_production

You are now ready to deploy to production.

(These tutorials indicate that you should select the Leave Extra files on destination (do not delete) option in the Publish profile. If you decide to clear that checkbox so that an update deployment will delete unneeded files in production, make sure that you put a copy of app_offline.htm in your project folder before you deploy. Otherwise Web Deploy will delete app_offline.htm during deployment, possibly before you are ready for it to be removed.)

Deploying the Code Update to the Test Environment

In the Solution Configurations drop-down box, select Release build configuration and in the Publish profile drop-down box select Production. Then click Publish Web.

Selecting_Release_and_Production

Visual Studio deploys the updated application and reports success in the Output window.

Output_window_after_deployment

Before you can test to verify successful deployment, you must remove the app_offline.htm file.

Return to the File Manager application in the control panel. Select contosouniversity.com and wwwroot, select app_offline.htm, and then click Delete.

Deleting_app_offline.htm

Now open a browser and browse to Instructors.aspx in the public site. Click a Select link to verify that the update was successfully deployed.

Instructors_page_with_courses_Prod

You've now deployed an application update that did not involve a database change. The next tutorial shows you how to deploy a database change.

Tom Dykstra

By Tom Dykstra, Tom Dykstra is a Senior Programming Writer on Microsoft's Web Platform & Tools Content Team.