在 Azure 中创建 ASP.NET Core Web 应用Create an ASP.NET Core web app in Azure

Azure 应用服务提供高度可缩放、自修补的 Web 托管服务。Azure App Service provides a highly scalable, self-patching web hosting service. 本快速入门演示如何将第一个 ASP.NET Core Web 应用部署到 Azure 应用服务中。This quickstart shows how to deploy your first ASP.NET Core web app to Azure App Service. 完成后,将拥有一个资源组,该资源组包含一个应用服务计划和一个部署了 Web 应用程序的应用服务应用。When you're finished, you'll have a resource group that consists of an App Service plan and an App Service app with a deployed web application.

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

先决条件Prerequisites

若要完成本教程,请安装带有 ASP.NET 和 Web 开发工作负荷的 Visual Studio 2017To complete this tutorial, install Visual Studio 2017 with the ASP.NET and web development workload.

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

  • 单击“帮助” > “检查更新”,在 Visual Studio 中安装最新更新。Install the latest updates in Visual Studio by clicking Help > Check for Updates.
  • 单击“工具” > “获取工具和功能”添加工作负荷。Add the workload by clicking Tools > Get Tools and Features.

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

在 Visual Studio 中,通过依次选择“文件”>“新建”>“项目”创建项目。In Visual Studio, create a project by selecting File > New > Project.

在“新建项目”对话框中,选择“Visual C#”>“Web”>“ASP.NET Core Web 应用程序”。In the New Project dialog, select Visual C# > Web > ASP.NET Core Web Application.

将应用程序命名为 myFirstAzureWebApp,然后选择“确定”。Name the application myFirstAzureWebApp, and then select OK.

“新建项目”对话框

可将任何类型的 ASP.NET Core Web 应用部署到 Azure。You can deploy any type of ASP.NET Core web app to Azure. 对于本快速入门,请选择“Web 应用程序”模板,确保身份验证设置为“无身份验证”,不要选择其他选项。For this quickstart, select the Web Application template, and make sure authentication is set to No Authentication and no other option is selected.

选择“确定” 。Select OK.

“新建 ASP.NET 项目”对话框

在菜单中,选择“调试>启动但不调试”以在本地运行 Web 应用。From the menu, select Debug > Start without Debugging to run the web app locally.

在本地运行应用

启动发布向导Launch the publish wizard

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

从解决方案资源管理器发布

发布向导将自动启动。The publish wizard is automatically launched. 选择“应用服务” > “发布”以打开“创建应用服务”对话框。Select App Service > Publish to open the Create App Service dialog.

从项目概述页发布

登录 AzureSign in to Azure

在“创建应用服务”对话框中单击“添加帐户”,然后登录到 Azure 订阅。In the Create App Service dialog, click Add an account, and sign in to your Azure subscription. 如果已登录,请从下拉列表中选择所需的帐户。If you're already signed in, select the account you want from the dropdown.

Note

如果已经登录,请先不要选择“创建”。If you're already signed in, don't select Create yet.

登录 Azure

创建资源组Create a resource group

资源组是在其中部署和管理 Azure 资源(例如 Web 应用、数据库和存储帐户)的逻辑容器。A resource group is a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed.

在“资源组”旁边,选择“新建”。Next to Resource Group, select New.

将资源组命名为 myResourceGroup,然后选择“确定”。Name the resource group myResourceGroup and select OK.

创建应用服务计划Create an App Service plan

应用服务计划指定托管应用的 Web 服务器场的位置、大小和功能。An App Service plan specifies the location, size, and features of the web server farm that hosts your app. 可以在托管多个应用时节省资金,方法是将多个 Web 应用配置为共用单个应用服务计划。You can save money when hosting multiple apps by configuring the web apps to share a single App Service plan.

应用服务计划定义:App Service plans define:

  • 区域(例如:中国东部、中国东部 2、中国北部、中国北部 2)Region (for example: China East, China East 2, China North, China North 2)
  • 实例大小(小、中、大)Instance size (small, medium, or large)
  • 规模计数(1 到 20 个实例)Scale count (1 to 20 instances)
  • SKU(免费、共享、基本、标准、高级)SKU (Free, Shared, Basic, Standard, or Premium)

在“托管计划”旁边,选择“新建”。Next to Hosting Plan, select New.

在“配置托管计划”对话框中,使用该屏幕截图下面的表中的设置。In the Configure Hosting Plan dialog, use the settings in the table following the screenshot.

创建应用服务计划

设置Setting 建议的值Suggested Value 描述Description
应用服务计划App Service Plan myAppServicePlanmyAppServicePlan 应用服务计划的名称。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.

选择“确定” 。Select OK.

创建并发布 Web 应用Create and publish the web app

在“应用名称”中,键入唯一的应用名称(有效字符为 a-z0-9-),或接受自动生成的唯一名称。In App Name, type a unique app name (valid characters are a-z, 0-9, and -), or accept the automatically generated unique name. Web 应用的 URL 为 http://<app_name>.chinacloudsites.cn,其中 <app_name> 是应用名称。The URL of the web app is http://<app_name>.chinacloudsites.cn, where <app_name> is your app name.

选择“创建”开始创建 Azure 资源。Select Create to start creating the Azure resources.

配置应用名称

向导完成后,它会将 ASP.NET Core Web 应用发布到 Azure,然后在默认浏览器中启动该应用。Once the wizard completes, it publishes the ASP.NET Core web app to Azure, and then launches the app in the default browser.

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

创建和发布步骤中指定的应用名称用作 http://<app_name>.chinacloudsites.cn 格式的 URL 前缀。The app name specified in the create and publish step is used as the URL prefix in the format http://<app_name>.chinacloudsites.cn.

恭喜,ASP.NET Core Web 应用已在 Azure 应用服务中实时运行!Congratulations, your ASP.NET Core web app is running live in Azure App Service.

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

在“解决方案资源管理器”中打开“Pages/Index.cshtml”。From the Solution Explorer, open Pages/Index.cshtml.

将两个 <div> 标记替换为以下代码:Replace the two <div> tags 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>

若要重新部署到 Azure,请在“解决方案资源管理器”中右键单击“myFirstAzureWebApp”项目,然后选择“发布”。To redeploy to Azure, right-click the myFirstAzureWebApp project in Solution Explorer and select Publish.

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

发布完成后,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

转到 Azure 门户管理 Web 应用。Go to the Azure portal to manage the web app.

从左侧菜单中选择“应用服务”,然后选择 Azure Web 应用的名称。From the left menu, select App Services, and then select the name of your Azure web app.

在门户中导航到 Azure Web 应用

随后会显示 Web 应用的概述页。You see your web 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

在前面的步骤中,在资源组中创建了 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