在 Azure 中建立 ASP.NET Core Web 應用程式Create an ASP.NET Core web app in Azure

注意

本文會將應用程式部署至 Windows 上的 App Service。This article deploys an app to App Service on Windows. 若要部署至 Linux 上的 App Service,請參閱在 Linux 上的 App Service 中建立 NET Core Web 應用程式To deploy to App Service on Linux, see Create a .NET Core web app in App Service on Linux.

Azure App Service 可提供可高度擴充、自我修復的 Web 主控服務。Azure App Service provides a highly scalable, self-patching web hosting service. 本快速入門會顯示如何將第一個 ASP.NET Core Web 應用程式部署至 Azure App Service。This quickstart shows how to deploy your first ASP.NET Core web app to Azure App Service. 當您完成時,您會有已部署 Web 應用程式的資源群,其中包含 App Service 方案和 App Service 應用程式。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 free account before you begin.

必要條件Prerequisites

若要完成此教學課程,請安裝 Visual Studio 2017ASP.NET 以及 Web 開發工作負載。To 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.

New Project dialog box

您可以將任何類型的 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. 選取 [App Service] > [發佈] 以開啟 [建立 App Service] 對話方塊。Select App Service > Publish to open the Create App Service dialog.

從專案概觀頁面發佈

登入 AzureSign in to Azure

在 [建立 App Service] 對話方塊中,按一下 [新增帳戶],然後登入您的 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.

注意

如果您已經登入,請勿選取 [建立]。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. 例如,您可以選擇在稍候透過一個簡單的步驟刪除整個資源群組。For example, you can choose to delete the entire resource group in one simple step later.

選取 [資源群組] 旁邊的 [新增]。Next to Resource Group, select New.

將資源群組命名為 myResourceGroup,然後選取 [確定]。Name the resource group myResourceGroup and select OK.

建立應用程式服務方案Create an App Service plan

App Service 方案會指定用來裝載應用程式的 Web 伺服器陣列位置、大小和功能。An App Service plan specifies the location, size, and features of the web server farm that hosts your app. 在裝載多個應用程式時,您可以將 Web 應用程式設定為共用單一 App Service 方案來節省開支。You can save money when hosting multiple apps by configuring the web apps to share a single App Service plan.

App Service 方案可定義:App Service plans define:

  • 區域 (例如:北歐、美國東部或東南亞)Region (for example: North Europe, East US, or Southeast Asia)
  • 執行個體大小 (小型、中型或大型)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.

建立 App Service 方案

設定Setting 建議的值Suggested Value 說明Description
App Service 方案App Service Plan myAppServicePlanmyAppServicePlan App Service 方案的名稱。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>.azurewebsites.net,其中 <app_name> 是您的應用程式名稱。The URL of the web app is http://<app_name>.azurewebsites.net, 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 應用程式

建立及發佈步驟中指定的應用程式名稱會作為 URL 首碼,其格式為 http://<app_name>.azurewebsites.netThe app name specified in the create and publish step is used as the URL prefix in the format http://<app_name>.azurewebsites.net.

恭喜您,您的 ASP.NET Core Web 應用程式在 Azure App Service 中即時執行。Congratulations, your ASP.NET Core web app is running live in Azure App Service.

更新應用程式並重新部署Update the app and redeploy

從 [方案總管],開啟 Pages/Index.cshtmlFrom 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 應用程式的名稱。From the left menu, select App Services, and then select the name of your Azure app.

入口網站瀏覽至 Azure 應用程式

您會看到 Web 應用程式的 [概觀] 頁面。You see your web app's Overview page. 您可以在這裡執行基本管理工作,像是瀏覽、停止、啟動、重新啟動及刪除。Here, you can perform basic management tasks like browse, stop, start, restart, and delete.

Azure 入口網站中的 App Service 刀鋒視窗

左側功能表提供不同的頁面來設定您的應用程式。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