"
ASP.NET (snapshot 2017) Microsoft documentation and samples

ASP.NET Web Deployment using Visual Studio: Command Line Deployment

by Tom Dykstra

Download Starter Project

This tutorial series shows you how to deploy (publish) an ASP.NET web application to Azure App Service Web Apps or to a third-party hosting provider, by using Visual Studio 2012 or Visual Studio 2010. For information about the series, see the first tutorial in the series.

Overview

This tutorial shows you how to invoke the Visual Studio web publish pipeline from the command line. This is useful for scenarios where you want to automate the deployment process instead of doing it manually in Visual Studio, typically by using a source code version control system.

Make a change to deploy

Currently the About page displays the template code.

About page with template code
About page with template code

You’ll replace that with code that displays a summary of student enrollment.

Open the About.aspx page, delete all of the markup inside the MainContent Content element, and insert the following markup in its place:

[!code-aspxMain]

   1:  <h2>Student Body Statistics</h2>
   2:      <asp:ObjectDataSource ID="StudentStatisticsObjectDataSource" runat="server" TypeName="ContosoUniversity.BLL.SchoolBL"
   3:          SelectMethod="GetStudentStatistics" DataObjectTypeName="ContosoUniversity.DAL.EnrollmentDateGroup">
   4:      </asp:ObjectDataSource>
   5:      <asp:GridView ID="StudentStatisticsGridView" runat="server" AutoGenerateColumns="False"
   6:          DataSourceID="StudentStatisticsObjectDataSource">
   7:          <Columns>
   8:              <asp:BoundField DataField="EnrollmentDate" DataFormatString="{0:d}" HeaderText="Date of Enrollment"
   9:                  ReadOnly="True" SortExpression="EnrollmentDate" />
  10:              <asp:BoundField DataField="StudentCount" HeaderText="Students" ReadOnly="True"
  11:                  SortExpression="StudentCount" />
  12:          </Columns>
  13:      </asp:GridView>

Run the project and select the About page.

About page
About page

Deploy to Test by using the command line

You won’t be deploying another database change, so disable dbDacFx database deployment for the aspnet-ContosoUniversity database. Open the Publish Web wizard, and in each of the three publish profiles, clear the Update Database check box on the Settings tab.

In the Windows 8 Start page, search for Developer Command Prompt for VS2012.

Right-click the icon for Developer Command Prompt for VS2012 and click Run as administrator.

Enter the following command at the command prompt, replacing the path to the solution file with the path to your solution file:

[!code-consoleMain]

   1:  msbuild C:\ContosoUniversity\ContosoUniversity.sln /p:DeployOnBuild=true /p:PublishProfile=Test

MSBuild builds the solution and deploys it to the test environment.

Command line output
Command line output

Open a browser and go to http://localhost/ContosoUniversity, then click the About page to verify that the deployment was successful.

If you haven’t created any students in test, you’ll see an empty page under the Student Body Statistics heading. Go to the Students page, click Add Student, and add some students, and then return to the About page to see student statistics.

About page in Test environment
About page in Test environment

Key command line options

The command that you entered passed the solution file path and two properties to MSBuild:

[!code-consoleMain]

   1:  msbuild C:\ContosoUniversity\ContosoUniversity.sln /p:DeployOnBuild=true 
   2:  /p:PublishProfile=Test

Deploying the solution versus deploying individual projects

Specifying the solution file causes all projects in the solution to be built. If you have multiple web projects in the solution, the following MSBuild behavior applies:

If you specify an individual project instead of a solution, you have to add a parameter that specifies the Visual Studio version. If you are using Visual Studio 2012 the command line would be similar to the following example:

[!code-consoleMain]

   1:  msbuild C:\ContosoUniversity\ContosoUniversity\ContosoUniversity.csproj /p:DeployOnBuild=true /p:PublishProfile=Test /p:VisualStudioVersion=11.0

The version number for Visual Studio 2010 is 10.0. For more information, see Visual Studio project compatability and VisualStudioVersion on Sayed Hashimi’s blog.

Specifying the publish profile

You can specify the publish profile by name or by the full path to the .pubxml file, as shown in the following example:

[!code-consoleMain]

   1:  msbuild C:\ContosoUniversity\ContosoUniversity.sln /p:DeployOnBuild=true /p:PublishProfile=C:\ContosoUniversity\ContosoUniversity\Properties\PublishProfiles\Test.pubxml

Web publish methods supported for command-line publishing

Three publish methods are supported for command line publishing:

Specifying the build configuration and platform

The build configuration and platform must be set in Visual Studio or on the command line. The publish profiles include properties that are named LastUsedBuildConfiguration and LastUsedPlatform, but you can’t set these properties in order to determine how the project is built. For more information, see MSBuild: how to set the configuration property on Sayed Hashimi’s blog.

Deploy to staging

To deploy to Azure, you must add the password to the command line. If you saved the password in the publish profile in Visual Studio, it was stored in encrypted form in the your .pubxml.user file. That file is not accessed by MSBuild when you do a command line deployment, so you have to pass in the password in a command line parameter.

  1. Copy the password that you need from the .publishsettings file that you downloaded earlier for the staging web site. The password is the value of the userPWD attribute for the Web Deploy publishProfile element.

    Web Deploy password
  2. In the Windows 8 Start page, search for Developer Command Prompt for VS2012, and click the icon to open the command prompt. (You don’t have to open it as administrator this time because you aren’t deploying to IIS on the local computer.)
  3. Enter the following command at the command prompt, replacing the path to the solution file with the path to your solution file and the password with your password:

    [!code-consoleMain]

       1:  msbuild C:\ContosoUniversity\ContosoUniversity.sln /p:DeployOnBuild=true /p:PublishProfile=Staging /p:Password=hdNSWsbuqno7J5uqnwKafwlfNPt1DSco7J5uqnwKafwlfNPt1DSpKHuYgCco7J5 /p:AllowUntrustedCertificate=true

    Notice that this command line includes an extra parameter: /p:AllowUntrustedCertificate=true. As this tutorial is being written, the AllowUntrustedCertificate property must be set when you publish to Azure from the command line. When the fix for this bug is released, you won’t need that parameter.
  4. Open a browser and go to the URL of your staging site, and then click the About page to verify that the deployment was successful.

    As you saw earlier for the test environment, you might have to create some students to see statistics on the About page.

Deploy to production

The process for deploying to production is similar to the process for staging.

  1. Copy the password that you need from the .publishsettings file that you downloaded earlier for the production web site.
  2. Open Developer Command Prompt for VS2012.
  3. Enter the following command at the command prompt, replacing the path to the solution file with the path to your solution file and the password with your password:

    [!code-consoleMain]

       1:  msbuild C:\ContosoUniversity\ContosoUniversity.sln /p:DeployOnBuild=true /p:PublishProfile=Production /p:Password=hdNSWsbuqnwKafwlo7J5uqnwKafwlfNPt1DSqnwKafwlfNPt1DSpKHuYgCco7J5 /p:AllowUntrustedCertificate=true

    For a real production site, if there was also a database change, you would typically copy the app_offline.htm file to the site before deployment and delete it after successful deployment.
  4. Open a browser and go to the URL of your staging site, and then click the About page to verify that the deployment was successful.

Summary

You have now deployed an application update by using the command line.

About page in Test environment
About page in Test environment

In the next tutorial, you will see an example of how to extend the web publish pipeline. The example will show you how to deploy files that are not included in the project.

Previous Next



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/command-line-deployment.htm
< THANKS ME>