通过在 Visual Studio 中导入发布设置将应用程序发布到 Azure 应用服务Publish an application to Azure App Service by importing publish settings in Visual Studio

可使用“发布”工具导入发布设置,然后部署应用。You can use the Publish tool to import publish settings and then deploy your app. 在本文中,我们使用 Azure 应用服务的发布设置,但你可以使用类似的步骤从 IIS 导入发布设置。In this article, we use publish settings for Azure App Service, but you can use similar steps to import publish settings from IIS. 在某些情况下,对于每个 Visual Studio 安装,使用发布设置配置文件比为服务手动配置部署要快。In some scenarios, use of a publish settings profile can be faster than manually configuring deployment to the service for each installation of Visual Studio.

这些步骤适用于 Visual Studio 中的 ASP.NET、ASP.NET Core 和 .NET Core 应用。These steps apply to ASP.NET, ASP.NET Core, and .NET Core apps in Visual Studio. 也可以为 Python 应用导入发布设置。You can also import publish settings for Python apps.

在本教程中,你将:In this tutorial, you will:

  • 从 Azure 应用服务生成发布设置文件Generate a publish settings file from Azure App Service
  • 将发布设置文件导入 Visual StudioImport the publish settings file into Visual Studio
  • 将应用部署到 Azure 应用服务Deploy the app to Azure App Service

发布设置文件 (*.publishsettings) 与在 Visual Studio 中创建的发布配置文件 (*.pubxml) 不同 。A publish settings file (*.publishsettings) is different than a publishing profile (*.pubxml) created in Visual Studio. 发布设置文件由 Azure 应用服务创建,然后可将其导入 Visual Studio。A publish settings file is created by Azure App Service, and then it can be imported into Visual Studio.

备注

若只需要将 Visual Studio 发布配置文件(*.pubxml 文件)从一个 Visual Studio 安装复制到另一个,则对于托管项目类型可以在 \<projectname>\Properties\PublishProfiles 文件夹中查找发布配置文件 <profilename>.pubxml 。If you just need to copy a Visual Studio publishing profile (*.pubxml file) from one installation of Visual Studio to another, you can find the publishing profile, <profilename>.pubxml, in the \<projectname>\Properties\PublishProfiles folder for managed project types. 对于网站则在 \App_Data 文件夹下进行查找。For websites, look under the \App_Data folder. 发布配置文件是 MSBuild XML 文件。The publishing profiles are MSBuild XML files.

先决条件Prerequisites

  • 必须安装 Visual Studio 2019 且具有 ASP.NET 和 web 开发工作负载。You must have Visual Studio 2019 installed and the ASP.NET and web development workload.

    如果尚未安装 Visual Studio,请转到 Visual Studio 下载页免费安装。If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.

  • 须安装 Visual Studio 2017 且具有 ASP.NET 和 web 开发工作负载。You must have Visual Studio 2017 installed and the ASP.NET and web development workload.

    如果尚未安装 Visual Studio,请转到 Visual Studio 下载页免费安装。If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.

在 Visual Studio 中新建 ASP.NET 项目Create a new ASP.NET project in Visual Studio

  1. 在运行 Visual Studio 的计算机上,创建新项目。On the computer running Visual Studio, create a new project.

    选择正确的模板。Choose the correct template. 在此示例中,选择“ASP.NET Web 应用程序(.NET Framework)”或“ASP.NET Core Web 应用程序”(仅限 C#),然后选择“确定” 。In this example, choose either ASP.NET Web Application (.NET Framework) or (for C# only) ASP.NET Core Web Application, and then select OK.

    如果没有看到指定的项目模板,请转到“新建项目”对话框左侧窗格中的“打开 Visual Studio 安装程序”链接 。If you don't see the specified project templates, go to the Open Visual Studio Installer link in the left pane of the New Project dialog box. Visual Studio 安装程序启动。The Visual Studio Installer launches. 安装“ASP.NET 和 Web 开发”工作负载。Install the ASP.NET and web development workload.

    选择的项目模板(ASP.NET 或 ASP.NET Core)必须与安装在 Web 服务器上的 ASP.NET 版本相对应。The project template you choose (ASP.NET or ASP.NET Core) must correspond to the version of ASP.NET installed on the web server.

  2. 选择“MVC”(.NET Framework) 或“Web 应用程序(模型-视图-控制器)”(适用于 .NET Core),并确保已选中“无身份验证”,然后选择“确定” 。Choose either MVC (.NET Framework) or Web Application (Model-View-Controller) (for .NET Core), and make sure that No Authentication is selected, and then select OK.

  3. 键入名称(例如“MyWebApp”),然后选择“确定” 。Type a name like MyWebApp and select OK.

    Visual Studio 随即创建项目。Visual Studio creates the project.

  4. 选择“生成” > “生成解决方案”以生成项目 。Choose Build > Build Solution to build the project.

在 Azure 应用服务中创建发布设置文件Create the publish settings file in Azure App Service

  1. 在 Azure 门户中打开 Azure 应用服务。In the Azure portal, open the Azure App Service.

  2. 转到“获取发布配置文件”并在本地保存配置文件。Go to Get publish profile and save the profile locally.

    获取发布配置文件

    已在保存该文件的位置生成了带 .publishsettings 文件扩展名的文件。A file with a .publishsettings file extension has been generated in the location where you saved it. 以下代码显示了该文件的部分示例(格式更易读)。The following code shows a partial example of the file (in a more readable formatting).

    <publishData>
      <publishProfile
        profileName="DeployASPDotNetCore - Web Deploy"
        publishMethod="MSDeploy"
        publishUrl="deployaspdotnetcore.scm.azurewebsites.net:443"
        msdeploySite="DeployASPDotNetCore"
        userName="$DeployASPDotNetCore"
        userPWD="abcdefghijklmnopqrstuzwxyz"
        destinationAppUrl="http://deployaspdotnetcore20180508031824.azurewebsites.net"
        SQLServerDBConnectionString=""
        mySQLDBConnectionString=""
        hostingProviderForumLink=""
        controlPanelLink="http://windows.azure.com"
        webSystem="WebSites">
        <databases />
      </publishProfile>
    </publishData>
    

    通常,前面的 *.publishsettings 文件包含两个可在 Visual Studio 中使用的发布配置文件:一个使用 Web 部署进行部署,另一个使用 FTP 进行部署。Typically, the preceding *.publishsettings file contains two publishing profiles that you can use in Visual Studio, one to deploy using Web Deploy, and one to deploy using FTP. 前面的代码显示了 Web 部署配置文件。The preceding code shows the Web Deploy profile. 导入配置文件时,稍后将导入两个配置文件。Both profiles will be imported later when you import the profile.

在 Visual Studio 中导入发布设置并进行部署Import the publish settings in Visual Studio and deploy

  1. 如果计算机上的 Visual Studio 中打开有 ASP.NET 项目,则在该计算机的解决方案资源管理器中右键单击该项目,然后选择“发布”。On the computer where you have the ASP.NET project open in Visual Studio, right-click the project in Solution Explorer, and choose Publish.

    如果先前配置了任何发布配置文件,则“发布”窗格会显示。If you have previously configured any publishing profiles, the Publish pane appears. 单击“新建”或“创建新配置文件”。Click New or Create new profile.

  2. 选择该选项以导入配置文件。Select the option to import a profile.

    在“发布”对话框中,单击“导入配置文件”。In the Publish dialog box, click Import Profile.

    在“选取发布目标”对话框中,点击“导入配置文件” 。In the Pick a publish target dialog box, click Import Profile.

    选择发布

  3. 导航到上一节中创建的发布设置文件的位置。Navigate to the location of the publish settings file that you created in the previous section.

  4. 在“导入发布设置文件”对话框中,导航到在上一部分创建的配置文件并选择该文件,然后单击“打开” 。In the Import Publish Settings File dialog, navigate to and select the profile that you created in the previous section, and click Open.

    单击“完成”保存发布配置文件,然后单击“发布”。Click Finish to save the publishing profile, and then click Publish.

    Visual Studio 开始执行部署过程,并且输出窗口将显示进度和结果。Visual Studio begins the deployment process, and the Output window shows progress and results.

    如果出现任何部署错误,请单击“编辑”以编辑设置。If you get an any deployment errors, click Edit to edit settings. 修改设置,然后单击“验证”以测试新设置。Modify settings and click Validate to test new settings. 如果找不到主机名,请尝试“服务器”和“目标 URL”字段中的 IP 地址而不是主机名 。If the host name is not found, try the IP address instead of the host name in the Server and Destination URL fields.

    Visual Studio 开始执行部署过程,并且输出窗口将显示进度和结果。Visual Studio begins the deployment process, and the Output window shows progress and results.

    如果出现任何部署错误,请单击“设置”以编辑设置。If you get an any deployment errors, click Settings to edit settings. 修改设置,然后单击“验证”以测试新设置。Modify settings and click Validate to test new settings. 如果找不到主机名,请尝试“服务器”和“目标 URL”字段中的 IP 地址而不是主机名 。If the host name is not found, try the IP address instead of the host name in the Server and Destination URL fields.

    编辑发布工具中的设置

后续步骤Next steps

在本教程中,创建了发布设置文件,将其导入 Visual Studio,并将 ASP.NET 应用部署到 Azure 应用服务。In this tutorial, you created a publish settings file, imported it into Visual Studio, and deployed an ASP.NET app to Azure App Service. 建议了解 Visual Studio 中发布选项的概述。You may want an overview of publishing options in Visual Studio.