您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:在 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.

完成后,你将获得一个 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 an account for free.

  • 本快速入门将应用部署到 Windows 上的应用服务。This quickstart deploys an app to App Service on Windows. 若要部署到基于 Linux 的应用服务,请参阅在基于 Linux 的应用服务中创建 .NET Core Web 应用To deploy to App Service on Linux, see Create a .NET Core web app in App Service on Linux.

  • 安装带有 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.
    • 通过选择“工具” > “获取工具和功能”,添加工作负荷。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. 如果你尚未从 Visual Studio 登录到 Azure 帐户,请选择“添加帐户”或“登录”。If you haven't already signed-in to your Azure account from Visual Studio, select either Add an account or Sign in. 也可以创建免费 Azure 帐户。You can also create a free Azure account.

  2. 在“选择发布目标”对话框中,依次选择“应用服务”、“新建”、“创建配置文件”。In the Pick a publish target dialog box, choose App Service, select Create New, and then select Create Profile.

    选取发布目标

  3. 在“应用服务:新建”对话框中为应用提供全局唯一的名称,可以接受默认名称,也可以输入新名称。In the App Service: Create new dialog, provide a globally unique Name for your app by either accepting the default name, or entering a new name. 有效字符为:a-zA-Z0-9-Valid characters are: a-z, A-Z, 0-9, and -. 名称用作 Web 应用的 URL 前缀,采用 http://<app_name>.azurewebsites.net 格式。This Name is used as the URL prefix for your web app in the format http://<app_name>.azurewebsites.net.

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

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

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

  7. 在“托管计划:新建”对话框中,输入下表中指定的值: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 “西欧”West Europe 托管 Web 应用的数据中心。The datacenter where the web app is hosted.
    大小Size 免费Free 定价层确定托管功能。Pricing tier determines hosting features.

    创建新的托管计划

  8. 将“Application Insights”保留设置为“无”。Leave Application Insights set to None.

  9. 在“应用服务:新建”对话框中,选择“创建”开始创建 Azure 资源。In the App Service: Create new dialog box, select Create to start creating the Azure resources.

    创建新的应用服务

  10. 完成向导中的操作后,选择“发布”。Once the wizard completes, select Publish.

    将 Web 应用发布到 Azure

    Visual Studio 会将 ASP.NET Core Web 应用发布到 Azure,并在默认浏览器中启动该应用。Visual Studio publishes your ASP.NET Core web app to Azure, and launches the app in your 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.

在门户中导航到 Azure 应用

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 门户菜单或页中选择“资源组”, 然后在“资源组”页中选择“myResourceGroup”。 From the Azure portal menu or Home page, select Resource groups, and 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:

希望优化并节省云支出?Want to optimize and save on your cloud spending?