快速入门:在 Azure 中创建 ASP.NET Core Web 应用Quickstart: Create an ASP.NET Core web app in Azure

本快速入门介绍如何创建第一个 ASP.NET Core Web 应用并将其部署到 Azure 应用服务In this quickstart, you'll learn how to create and deploy your first ASP.NET Core web app to Azure App Service. 应用服务支持 .NET 5.0 应用。App Service supports .NET 5.0 apps.

完成后,你将获得一个 Azure 资源组,其中包含一个应用服务托管计划,以及一个部署了 Web 应用程序的应用服务。When you're finished, you'll have an Azure resource group consisting of an App Service hosting plan and an App Service with a deployed web application.

先决条件Prerequisites

  • 具有活动订阅的 Azure 帐户。An Azure account with an active subscription. 创建试用版订阅Create a Trial Subscription.

  • 安装带有 ASP.NET 和 Web 开发工作负荷的 Visual Studio 2019Install Visual Studio 2019 with the ASP.NET and web development workload.

    如果已安装 Visual Studio 2019:If you've installed Visual Studio 2019 already:

    • 通过选择“帮助” > “检查更新”,在 Visual Studio 中安装最新更新。Install the latest updates in Visual Studio by selecting Help > Check for Updates. 最新的更新包含 .NET 5.0 SDK。The latest updates contain the .NET 5.0 SDK.
    • 通过选择“工具” > “获取工具和功能”,添加工作负荷。Add the workload by selecting Tools > Get Tools and Features.

创建一个 ASP.NET Core Web 应用Create an ASP.NET Core web app

遵循以下步骤在 Visual Studio 中创建 ASP.NET Core Web 应用:Create an ASP.NET Core web app in Visual Studio by following these steps:

  1. 打开 Visual Studio 并选择“创建新项目”。Open Visual Studio and select Create a new project.

  2. 在“创建新项目”中选择“ASP.NET Core Web 应用程序”,确认“C#”列在所选内容的语言中,然后选择“下一步”。In Create a new project, select ASP.NET Core Web Application and confirm that C# is listed in the languages for that choice, then select Next.

  3. 在“配置新项目”中,将 Web 应用程序项目命名为 myFirstAzureWebApp,然后选择“创建”。In Configure your new project, name your web application project myFirstAzureWebApp, and select Create.

    配置 Web 应用项目

  4. 可将任何类型的 ASP.NET Core Web 应用部署到 Azure,但对于本快速入门,请选择“Web 应用程序”模板。You can deploy any type of ASP.NET Core web app to Azure, but for this quickstart, choose the Web Application template. 确保“身份验证”设置为“无身份验证”,并且未选择其他选项。Make sure Authentication is set to No Authentication, and that no other option is selected. 然后选择“创建”。Then, select Create.

    创建新的 ASP.NET Core Web 应用

  5. 在 Visual Studio 菜单中,选择“调试” > “开始执行(不调试)”以在本地运行 Web 应用。From the Visual Studio menu, select Debug > Start Without Debugging to run your web app locally.

    Web 应用在本地运行

发布 Web 应用Publish your web app

若要发布 Web 应用,必须先创建并配置一个可将应用发布到的新应用服务。To publish your web app, you must first create and configure a new App Service that you can publish your app to.

在设置应用服务的过程中,将会创建:As part of setting up the App Service, you'll create:

  • 一个新的资源组,用于包含该服务的所有 Azure 资源。A new resource group to contain all of the Azure resources for the service.
  • 一个新的托管计划,用于指定托管应用的 Web 服务器场的位置、大小和功能。A new Hosting Plan that specifies the location, size, and features of the web server farm that hosts your app.

遵循以下步骤创建应用服务并发布 Web 应用:Follow these steps to create your App Service and publish your web app:

  1. 在“解决方案资源管理器”中右键单击“myFirstAzureWebApp”项目,然后选择“发布”。In Solution Explorer, right-click the myFirstAzureWebApp project and select Publish.

  2. 在“发布”中,选择“Azure”,然后单击“下一步” 。In Publish, select Azure and click Next.

  3. 你的选项取决于你是否已登录 Azure 以及是否有一个链接到 Azure 帐户的 Visual Studio 帐户。Your options depend on whether you're signed in to Azure already and whether you have a Visual Studio account linked to an Azure account. 选择“添加帐户”或“登录”以登录 Azure 订阅。Select either Add an account or Sign in to sign in to your Azure subscription. 如果你已经登录,请选择所需的帐户。If you're already signed in, select the account you want.

    登录到 Azure

  4. 在“应用服务实例”的右侧,单击“+” 。To the right of App Service instances, click +.

    新的应用服务应用

  5. 对于“订阅”,请接受列出的订阅,或从下拉列表中选择一个新订阅。For Subscription, accept the subscription that is listed or select a new one from the drop-down list.

  6. 对于“资源组”,选择“新建”。For Resource group, select New. 在“新资源组名称”中,输入“myResourceGroup”并选择“确定”。In New resource group name, enter myResourceGroup and select OK.

  7. 对于“托管计划”,请选择“新建”。For Hosting Plan, select New.

  8. 在“托管计划:新建”对话框中,输入下表中指定的值:In the Hosting Plan: Create new dialog, enter the values specified in the following table:

    设置Setting 建议的值Suggested Value 说明Description
    托管计划Hosting Plan myFirstAzureWebAppPlanmyFirstAzureWebAppPlan 应用服务计划的名称。Name of the App Service plan.
    位置Location 中国东部China East 托管 Web 应用的数据中心。The datacenter where the web app is hosted.
    大小Size 免费Free 定价层确定托管功能。Pricing tier determines hosting features.

    创建新的托管计划

  9. 名称 中,输入仅包含有效字符 a-zA-Z0-9- 的唯一应用名称。In Name, enter a unique app name that includes only the valid characters are a-z, A-Z, 0-9, and -. 可以接受自动生成的唯一名称。You can accept the automatically generated unique name. Web 应用的 URL 为 http://<app-name>.azurewebsites.net,其中 <app-name> 是应用名称。The URL of the web app is http://<app-name>.azurewebsites.net, where <app-name> is your app name.

  10. 选择“创建”以创建 Azure 资源。Select Create to create the Azure resources.

    创建应用资源

    向导完成后,便为你创建了 Azure 资源,你就可以进行发布了。Once the wizard completes, the Azure resources are created for you and you are ready to publish.

  11. 选择“完成”关闭向导。Select Finish to close the wizard.

  12. 在“发布”页中,单击“发布”。In the Publish page, click Publish. Visual Studio 将生成、打包应用并将其发布到 Azure,然后在默认浏览器中启动该应用。Visual Studio builds, packages, and publishes the app to Azure, and then launches the app in the default browser.

    已发布 Azure 中运行的 ASP.NET Web 应用

祝贺你! Congratulations! 你的 ASP.NET Core Web 应用已在 Azure 应用服务中实时运行!Your ASP.NET Core web app is running live in Azure App Service.

更新应用并重新部署Update the app and redeploy

遵循以下步骤更新并重新部署 Web 应用:Follow these steps to update and redeploy your web app:

  1. 解决方案资源管理器 中你的项目下,打开“页” > “Index.cshtml”。In Solution Explorer, under your project, open Pages > Index.cshtml.

  2. 将整个 <div> 标记替换为以下代码:Replace the entire <div> tag with the following code:

    <div class="jumbotron">
        <h1>ASP.NET in Azure!</h1>
        <p class="lead">This is a simple app that we've built that demonstrates how to deploy a .NET app to Azure App Service.</p>
    </div>
    
  3. 若要重新部署到 Azure,请在“解决方案资源管理器”中右键单击“myFirstAzureWebApp”项目,然后选择“发布”。To redeploy to Azure, right-click the myFirstAzureWebApp project in Solution Explorer and select Publish.

  4. 在“发布”摘要页中选择“发布” 。In the Publish summary page, select Publish.

    发布对 Web 应用的更新

发布完成后,Visual Studio 将启动浏览器并转到 Web 应用的 URL。When publishing completes, Visual Studio launches a browser to the URL of the web app.

已更新 Azure 中运行的 ASP.NET Web 应用

管理 Azure 应用Manage the Azure app

若要管理 Web 应用,请转到 Azure 门户,然后搜索并选择“应用服务”。To manage your web app, go to the Azure portal, and search for and select App Services.

选择应用服务

在“应用服务”页上,选择 Web 应用的名称。On the App Services page, select the name of your web app.

“应用程序服务”页面的屏幕截图,显示已选择一个示例 Web 应用。

Web 应用的“概述”页包含用于基本管理(例如浏览、停止、启动、重启和删除)的选项。The Overview page for your web app, contains options for basic management like browse, stop, start, restart, and delete. 左侧菜单提供用于配置应用的更多页面。The left menu provides further pages for configuring your app.

Azure 门户中的应用服务

清理资源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 门户菜单或“主页”页上,选择“资源组” 。From the Azure portal menu or Home page, select Resource groups . 然后,在“资源组”页上,选择“myResourceGroup” 。Then, on the Resource groups page, select myResourceGroup .

在“myResourceGroup”页中,确保列出的资源是要删除的资源。 On the myResourceGroup page, make sure that the listed resources are the ones you want to delete.

选择“删除资源组” ,在文本框中键入“myResourceGroup” 以确认,然后选择“删除” 。Select Delete resource group , type myResourceGroup in the text box to confirm, and then select Delete .

后续步骤Next steps

在本快速入门中,你已使用 Visual Studio 创建了一个 ASP.NET Core Web 应用,并已将其部署到 Azure 应用服务。In this quickstart, you used Visual Studio to create and deploy an ASP.NET Core web app to Azure App Service.

请继续学习下一篇文章,了解如何创建 .NET Core 应用并将其连接到 SQL 数据库:Advance to the next article to learn how to create a .NET Core app and connect it to a SQL Database:

Linux 应用服务使用 Linux 操作系统,提供高度可缩放的自修补 Web 托管服务。App Service on Linux provides a highly scalable, self-patching web hosting service using the Linux operating system. 本快速入门演示如何在 Linux 应用服务中创建 .NET Core 应用。This quickstart shows how to create a .NET Core app on App Service on Linux. 使用 Azure CLI 创建应用,并使用 Git 将 .NET Core 代码部署到该应用。You create the app using the Azure CLI, and you use Git to deploy the .NET Core code to the app.

在 Azure 中运行应用的示例

可以使用 Mac、Windows 或 Linux 计算机执行本文中的步骤。You can follow the steps in this article using a Mac, Windows, or Linux machine.

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅If you don't have an Azure trail subscription, create a trial subscription before you begin.

设置初始环境Set up your initial environment

若要完成本快速入门教程,需先执行以下操作:To complete this quickstart:

存在问题?请告诉我们。Having issues? Let us know.

在本地创建应用Create the app locally

在计算机的终端窗口中,创建一个名为 hellodotnetcore 的目录,并将当前目录切换到该目录。In a terminal window on your machine, create a directory named hellodotnetcore and change the current directory to it.

mkdir hellodotnetcore
cd hellodotnetcore

创建新的 .NET Core 应用。Create a new .NET Core app.

dotnet new web

在本地运行应用Run the app locally

在本地运行应用程序,以便你能了解将它部署到 Azure 时它的外观应该是什么样的。Run the application locally so that you see how it should look when you deploy it to Azure.

dotnet run

打开 Web 浏览器并导航到 http://localhost:5000 处的应用。Open a web browser, and navigate to the app at http://localhost:5000.

页面中会显示该示例应用发出的 Hello World 消息。You see the Hello World message from the sample app displayed in the page.

使用浏览器进行测试

存在问题?请告诉我们。Having issues? Let us know.

登录 AzureSign into Azure

在终端窗口中,通过以下命令登录到 Azure:In your terminal window, log into Azure with the following command:

az login

部署应用Deploy the app

使用 az webapp up 命令在本地文件夹 (hellodotnetcore) 中部署代码:Deploy the code in your local folder (hellodotnetcore) using the az webapp up command:

az webapp up --sku F1 --name <app-name> --os-type linux
  • 如果无法识别 az 命令,请确保按照设置初始环境中所述安装 Azure CLI。If the az command isn't recognized, be sure you have the Azure CLI installed as described in Set up your initial environment.
  • <app-name> 替换为在整个 Azure 中均唯一的名称(有效字符为 a-z0-9-)。Replace <app-name> with a name that's unique across all of Azure (valid characters are a-z, 0-9, and -). 良好的模式是结合使用公司名称和应用标识符。A good pattern is to use a combination of your company name and an app identifier.
  • --sku F1 参数在“免费”定价层上创建 Web 应用。The --sku F1 argument creates the web app on the Free pricing tier. 省略此参数可使用更快的高级层,这会按小时计费。Omit this argument to use a faster premium tier, which incurs an hourly cost.
  • 可以选择包含参数 --location <location-name>,其中 <location-name> 是可用的 Azure 区域。You can optionally include the argument --location <location-name> where <location-name> is an available Azure region. 可以运行 az account list-locations 命令来检索 Azure 帐户的允许区域列表。You can retrieve a list of allowable regions for your Azure account by running the az account list-locations command.

此命令可能需要花费几分钟时间完成。The command may take a few minutes to complete. 运行时,它提供以下相关信息:创建资源组、应用服务计划和托管应用,配置日志记录,然后执行 ZIP 部署。While running, it provides messages about creating the resource group, the App Service plan and hosting app, configuring logging, then performing ZIP deployment. 然后,它将显示消息“可以在 http://<app-name>.azurewebsites.net(这是 Azure 上应用的 URL)启动应用”。It then gives the message, "You can launch the app at http://<app-name>.azurewebsites.net", which is the app's URL on Azure.

存在问题?请告诉我们。Having issues? Let us know.

备注

az webapp up 命令执行以下操作:The az webapp up command does the following actions:

浏览到应用Browse to the app

使用 Web 浏览器浏览到已部署的应用程序。Browse to the deployed application using your web browser.

http://<app_name>.chinacloudsites.cn

.NET Core 示例代码在包含内置映像的 Linux 上的应用服务中运行。The .NET Core sample code is running in App Service on Linux with a built-in image.

在 Azure 中运行应用的示例

祝贺你! Congratulations! 现已将第一个 .NET Core 应用部署到 Linux 应用服务。You've deployed your first .NET Core app to App Service on Linux.

存在问题?请告诉我们。Having issues? Let us know.

更新并重新部署代码Update and redeploy the code

在本地目录中,打开 Startup.cs 文件 。In the local directory, open the Startup.cs file. 对方法调用 context.Response.WriteAsync 中的文本稍作更改:Make a small change to the text in the method call context.Response.WriteAsync:

await context.Response.WriteAsync("Hello Azure!");

保存更改,然后再次使用 az webapp up 命令重新部署应用:Save your changes, then redeploy the app using the az webapp up command again:

az webapp up --os-type linux

此命令使用本地缓存在 .azure/config 文件中的值,包括应用名称、资源组和应用服务计划。This command uses values that are cached locally in the .azure/config file, including the app name, resource group, and App Service plan.

完成部署后,切换回在“浏览到应用” 步骤中打开的浏览器窗口,并点击“刷新”。Once deployment has completed, switch back to the browser window that opened in the Browse to the app step, and hit refresh.

已更新的在 Azure 中运行应用的示例

存在问题?请告诉我们。Having issues? Let us know.

管理新的 Azure 应用Manage your new Azure app

转到 Azure 门户管理已创建的应用。Go to the Azure portal to manage the app you created.

在左侧菜单中单击 应用程序服务,然后单击 Azure 应用的名称。From the left menu, click App Services, and then click the name of your Azure app.

“应用程序服务”的屏幕截图,显示已选择一个示例 Azure 应用。

这里我们可以看到应用的“概述”页。You see your app's Overview page. 并可以执行基本的管理任务,例如浏览、停止、启动、重新启动和删除。Here, you can perform basic management tasks like browse, stop, start, restart, and delete.

Azure 门户中的应用服务页

左侧菜单提供了用于配置应用的不同页面。The left menu provides different pages for configuring your app.

清理资源Clean up resources

若要清理资源,请运行以下命令:To clean up your resources, run the following command:

az group delete --name myResourceGroup

存在问题?请告诉我们。Having issues? Let us know.

后续步骤Next steps