使用 Visual Studio 将 Web 应用发布到 Azure 应用服务Publish a Web app to Azure App Service using Visual Studio

对于 ASP.NET、ASP.NET Core、Node.js 和 .NET Core 应用,请使用以下任一方法发布到 Azure 应用服务或 Azure 应用服务 Linux(使用容器)。For ASP.NET, ASP.NET Core, Node.js, and .NET Core apps, publish to Azure App Service or Azure App Service Linux (using containers) using one of the following methods.

  • 对于应用的连续(或自动)部署,请将 Azure DevOps 与 Azure 管道结合使用。For continuous (or automated) deployment of apps, use Azure DevOps with Azure Pipelines.

  • 对于应用的一次性(或手动)部署,请使用 Visual Studio 中的“发布”工具将 ASP.NET、ASP.NET Core、Node.js 和 .NET Core 应用部署到 Azure 应用服务或适用于 Linux 的应用服务(使用容器)。For one-time (or manual) deployment of apps, use the Publish tool in Visual Studio to deploy ASP.NET, ASP.NET Core, Node.js, and .NET Core apps to Azure App Service or App Service for Linux (using containers). 对于 Python 应用,请按照 Python - 发布到 Azure 应用服务上的步骤操作。For Python apps, follow the steps on Python - Publish to Azure App Service.

本文介绍如何使用“Publish”工具进行一次性部署。This article describes how to use the Publish tool for one-time deployment.

先决条件Prerequisites

  • 安装有 Visual Studio 2019 并具有所选语言相应的工作负载:Visual Studio 2019 installed with the appropriate workloads for your language of choice:
    • ASP.NET:ASP.NET 和 Web 开发ASP.NET: ASP.NET and web development
    • Node.js:Node.js 开发Node.js: Node.js development
  • 安装有 Visual Studio 2017并具有所选语言相应的工作负荷:Visual Studio 2017 installed with the appropriate workloads for your language of choice:
    • ASP.NET:ASP.NET 和 Web 开发ASP.NET: ASP.NET and web development
    • Node.js:Node.js 开发Node.js: Node.js development
  • Azure 订阅。An Azure subscription. 如果还没有订阅,请免费注册,其中包括为期 30 天的 $200 额度和为期 12 个月的热门免费服务。If you do not already have subscription, sign up for free, which includes $200 in credit for 30 days and 12 months of popular free services.

  • ASP.NET、ASP.NET Core、.NET Core 或 Node.js 项目。An ASP.NET, ASP.NET Core, .NET Core, or Node.js project. 如果还没有项目,请选择下方选项:If you don't already have a project, select an option below:

    • ASP.NET Core:按照快速入门:使用 Visual Studio 创建第一个 ASP.NET Core Web 应用的说明操作,或者使用以下步骤:ASP.NET Core: Follow Quickstart: Use Visual Studio to create your first ASP.NET Core web app, or use the following steps:

      在 Visual Studio 2019 中的“启动”窗口上,选择“新建项目”。In Visual Studio 2019, choose Create a new project in the start window. 如果开始窗口未打开,请选择“文件” > “开始窗口” 。If the start window is not open, choose File > Start Window. 在搜索框中键入“Web 应用”,选择“C#”作为语言,然后选择“ASP.NET Core Web 应用程序(模型-视图-控制器)”,再选择“下一步”。Type web app in the search box, choose C# as the language, then choose ASP.NET Core Web Application (Model-View-Controller), and then choose Next. 在下一个屏幕上,将项目命名为“MyASPApp”,然后选择“下一步”。On the next screen, name the project MyASPApp, and then choose Next.

      选择建议的目标框架 (.NET Core 3.1) 或 .NET 5,然后选择“创建”。Choose either the recommended target framework (.NET Core 3.1) or .NET 5, and then choose Create.

      在 Visual Studio 2017 中,选择“文件” > “新建项目”,然后选择“Visual C#” > “.NET Core”,再选择“ASP.NET Core Web 应用程序” 。In Visual Studio 2017, choose File > New Project, select Visual C# > .NET Core, then select ASP.NET Core Web Application. 系统出现提示时,请选择“Web 应用程序(模型-视图-控制器)”模板,确保选中“无身份验证”,然后选择“确定”。When prompted, select the Web Application (Model-View-Controller) template, make sure that No Authentication is selected, and then select OK.

    • Node.js:请遵照快速入门:使用 Visual Studio 创建首个 Node.js 应用进行操作,或使用“文件” > “新建文件”,选择“JavaScript”,然后选择“空白 Node.js Web 应用程序”。Node.js: Follow Quickstart: Use Visual Studio to create your first Node.js app, or use File > New Project, select JavaScript, then select Blank Node.js Web Application.

  • 请确保在执行部署步骤之前,使用“生成”>“生成解决方案”菜单命令生成项目。Make sure you build the project using the Build > Build Solution menu command before following the deployment steps.

发布到 Windows 上的 Azure 应用服务Publish to Azure App Service on Windows

  1. 在解决方案资源管理器中,右键单击该项目节点,并选择“发布”(或使用“生成” > “发布”菜单项) 。In Solution Explorer, right-click the project node and choose Publish (or use the Build > Publish menu item).

    解决方案资源管理器中项目上下文菜单上的“发布”命令The Publish command on the project context menu in Solution Explorer

  2. 如果先前配置了任何发布配置文件,则“发布”窗口会显示。If you have previously configured any publishing profiles, the Publish window appears. 选择“新建”。Select New.

  3. 在“发布”窗口中,选择“Azure”。In the Publish window, select Azure.

    选择发布目标

  4. 选择“Azure 应用服务(Windows)”,然后选择“下一步” 。Select Azure App Service (Windows) and Next.

    选择“Linux 上的 Azure 应用服务”

  5. 如有必要,请用 Azure 帐户登录。Sign in with your Azure account, if necessary. 选择“创建新的 Azure 应用服务…”Select Create a new Azure App Service...

    用于创建新的 Azure 应用服务实例的链接

  6. 在“创建 Azure 应用服务(Windows)”对话框中,填写“应用名称”、“资源组”和“应用服务计划”输入字段 。In the Create Azure App Service (Windows) dialog, the App Name, Resource Group, and App Service Plan entry fields are populated. 可以保留这些名称,也可以进行更改。You can keep these names or change them. 准备就绪后,选择“创建”。When ready, select Create.

    “创建 Azure 应用服务(Windows)”对话框的屏幕截图,其中已填写“名称”、“订阅”、“资源组”和“托管计划”字段。

  7. 在“发布”对话框中,将自动选择新创建的实例。In the Publish dialog, the newly created instance has been automatically selected. 准备就绪后,选择“完成”。When ready, select Finish.

    从 Visual Studio 解决方案资源管理器访问的“发布”窗口的屏幕截图。

  8. 选择“发布”。Select Publish. Visual Studio 将应用部署到 Azure 应用服务,并且 Web 应用将在浏览器中加载。Visual Studio deploys the app to your Azure App Service, and the web app loads in your browser. 项目属性“发布”窗格显示了站点 URL 和其他详细信息。The project properties Publish pane shows the site URL and other details.

    显示配置文件摘要的“发布”属性窗格

清理资源Clean up resources

在前面的步骤中,你在资源组中创建了 Azure 资源。In the preceding steps, you created Azure resources in a resource group. 如果以后不需要这些资源,可以通过删除资源组来删除它们。If you don't expect to need these resources in the future, you can delete them by deleting the resource group. 从 Azure 门户左侧菜单中,选择“资源组”,然后选择“myResourceGroup” 。From the left menu in the Azure portal, select Resource groups and then select myResourceGroup. 在资源组页上,确保列出的资源是要删除的。On the resource group page, make sure that the listed resources are the ones you want to delete. 选择“删除”,在文本框中键入“myResourceGroup”,然后选择“删除” 。Select Delete, type myResourceGroup in the text box, and then select Delete.

后续步骤Next steps

在本快速入门中,学习了如何使用 Visual Studio 创建用于部署到 Azure 的发布配置文件。In this quickstart, you learned how to use Visual Studio to create a publishing profile for deployment to Azure. 还可以通过从 Azure 应用服务导入发布设置来配置发布配置文件。You can also configure a publishing profile by importing publish settings from Azure App Service.