快速入門:部署 ASP.NET Web 應用程式

在此快速入門中,您將了解如何建立第一個 ASP.NET Web 應用程式,並將其部署至 Azure App Service。 App Service 支援各種版本的 .NET 應用程式,並提供可高度調整且自我修補的虛擬主機服務。 ASP.NET Web 應用程式是跨平台的應用程式,可以裝載在 Linux 或 Windows 上。 當您完成時,您會有一個 Azure 資源群組,其中包含 App Service 主控方案和已部署 Web 應用程式的 App Service。

或者,您也可以將 ASP.NET Web 應用程式部署為 App Service 中 Windows 或 Linux 容器的一部分。

這段影片示範如何部署 ASP.NET Web 應用程式。

下列各節也會說明影片中的步驟。

必要條件

若您已安裝 Visual Studio 2022:

  1. 選取 [說明]>[檢查更新],在 Visual Studio 中安裝最新的更新。
  2. 選取 [工具]>[取得工具和功能] 以新增工作負載。

1.建立 ASP.NET Web 應用程式

1.初始化 ASP.NET Web 應用程式範本

  1. 開啟 Visual Studio,然後選取 [建立新專案]

  2. 在 [建立新專案] 中,尋找並選取 [ASP.NET Core Web App] \(ASP.NET Core Web 應用程式\),然後選取 [下一步]

  3. 在 [設定新專案] 中,將應用程式命名為 MyFirstAzureWebApp,然後選取 [下一步]

    Screenshot of Visual Studio - Configure ASP.NET 7.0 web app.

  4. 選取 [.NET 7.0 (標準期限支援)]

  5. 確定將 [驗證類型] 設定為 [無]。 選取 建立

    Screenshot of Visual Studio - Additional info when selecting .NET 7.0.

  6. 從 Visual Studio 功能表中,選取 [偵錯]>[啟動但不偵錯] 以在本機執行 Web 應用程式。 如果出現訊息要求您信任自我簽署憑證,請選取 [是]

    Screenshot of Visual Studio - ASP.NET Core 7.0 running locally.

  1. 將您電腦上的終端機視窗開啟至工作目錄。 使用 dotnet new webapp 命令建立新的 .NET Web 應用程式,然後將目錄變更為新建立的應用程式。

    dotnet new webapp -n MyFirstAzureWebApp --framework net7.0
    cd MyFirstAzureWebApp
    
  2. 從相同的終端機工作階段中,使用 dotnet run 命令在本機執行應用程式。

    dotnet run --urls=https://localhost:5001/
    
  3. 開啟網頁瀏覽器,然後巡覽至位於 https://localhost:5001 的應用程式。

    您會看到頁面顯示範本 ASP.NET Core 7.0 Web 應用程式。

    Screenshot of Visual Studio Code - ASP.NET Core 7.0 in local browser.

在此步驟中,您將派生要部署的示範專案。

  1. 移至 .NET 7.0 範例應用程式
  2. 選取 GitHub 頁面右上方的 [派生] 按鈕。
  3. 選取 [擁有者],並保留預設 [存放庫名稱]
  4. 選取 [建立派生]

本快速入門使用 Azure Developer CLI (azd) 來建立 Azure 資源,並在其中部署程式碼。 如需 Azure Developer CLI 的詳細資訊,請瀏覽文件訓練路徑

使用下列步驟,擷取並初始化 ASP.NET Core Web 應用程式範本

  1. 將您電腦上的終端機視窗開啟至空的工作目錄。 使用 azd init 命令初始化 azd 範本。

    azd init --template https://github.com/Azure-Samples/quickstart-deploy-aspnet-core-app-service.git
    

    當系統提示您輸入環境名稱時,請輸入 dev

  2. 從相同的終端機工作階段中,使用 dotnet run 命令在本機執行應用程式。 使用 --project 參數來指定 azd 範本的 src 目錄,也就是應用程式程式碼的所在位置。

    dotnet run --project src --urls=https://localhost:5001/
    
  3. 開啟網頁瀏覽器,然後瀏覽至位於 https://localhost:5001 的應用程式。 頁面會顯示 ASP.NET Core 7.0 Web 應用程式範本。

    Screenshot of Visual Studio Code - ASP.NET Core 7.0 in local browser.

2.發佈 Web 應用程式

AZD 樣本所含的檔案會產生應用程式在 App Service 中執行所需的下列資源:

請遵循下列步驟來建立您的 App Service 資源,並發佈您的專案:

  1. 在 [方案總管] 中,以滑鼠右鍵按一下 [MyFirstAzureWebApp] 專案,然後選取 [發佈]

  2. 在 [發佈] 中選取 [Azure],然後選取 [下一步]

    Screenshot of Visual Studio - Publish the web app and target Azure.

  3. 選擇 [特定目標]Azure App Service (Linux)Azure App Servuce (Windows)。 然後選取下一步

    重要

    以 ASP.NET Framework 4.8 為目標時,請使用 Azure App Service (Windows)

  4. 您的選項取決於您是否已登入 Azure,以及是否已將 Visual Studio 帳戶連結至 Azure 帳戶。 選取 [新增帳戶] 或 [登入] 來登入 Azure 訂用帳戶。 若您已經登入,請選取要使用的帳戶。

    Screenshot of Visual Studio - Select sign in to Azure dialog.

  5. 在 [App Service 執行個體] 右側,選取 [+]。

    Screenshot of Visual Studio - New App Service app dialog.

  6. 針對 [訂用帳戶],請接受列出的訂用帳戶,或從下拉式清單中選取一個新的訂用帳戶。

  7. 在 [資源群組] 中選取 [新增]。 在 [新增資源群組名稱] 中,輸入 myResourceGroup,然後選取 [確定]

  8. 在 [主控方案] 中選取 [新增]

  9. 在 [主控方案:新建] 對話方塊中,輸入下表中指定的值:

    設定 建議的值 描述
    主控方案 MyFirstAzureWebAppPlan App Service 方案的名稱。
    地點 西歐 裝載 Web 應用程式的資料中心。
    大小 免費 定價層可決定裝載功能。
  10. 在 [名稱] 中,輸入唯一的應用程式名稱 (只包含 a-zA-Z0-9- 等有效字元)。 您可以接受自動產生的唯一名稱。 Web 應用程式的 URL 是 http://<app-name>.azurewebsites.net,其中 <app-name> 是您的應用程式名稱。

  11. 選取 [建立] 以建立 Azure 資源。

    Screenshot of Visual Studio - Create app resources dialog.

    一旦精靈完成,就會為您建立 Azure 資源,然後您就可以開始發佈您的 ASP.NET Core 專案。

  12. 在 [發佈] 對話方塊中,確定已選取新的 App Service 應用程式,然後依序選取 [完成] 和 [關閉]。 Visual Studio 會針對選取的 App Service 應用程式來為您建立發行設定檔。

  13. 在 [發佈] 頁面中,選取 [發佈]。 若您看到警示訊息,請選取 [繼續]

    Visual Studio 會建立和封裝應用程式,並將其發佈至 Azure,然後在預設瀏覽器中啟動應用程式。

    頁面會顯示 ASP.NET Core 7.0 Web 應用程式。

    Screenshot of Visual Studio - ASP.NET Core 7.0 web app in Azure.

  1. 在 Visual Studio Code 中,選取 [檢視]>[命令選擇區],以開啟 命令選擇區

  2. 搜尋並選取 [Azure App Service:建立新的 Web 應用程式 (進階)]。

  3. 如下所示地回應提示:

    1. 如果出現提示,登入您的 Azure 帳戶。
    2. 選取您的 [訂用帳戶]
    3. 選取 [建立新的 Web 應用程式...進階]。
    4. 針對 [Enter a globally unique name] \(輸入全域唯一名稱\),請使用所有 Azure 中的唯一名稱 (有效字元為 a-z0-9-)。 良好的模式是使用您的公司名稱和應用程式識別碼的組合。
    5. 選取 [建立新的資源群組],然後提供 myResourceGroup 之類的名稱。
    6. 系統提示您 [選取執行階段堆疊]時,請選取 [.NET 7 (STS)]
    7. 選取作業系統 (Windows 或 Linux)。
    8. 選取與您接近的位置。
    9. 選取 [建立新的 App Service 方案]、提供名稱,然後選取 [F1 Free] \(F1 免費\)定價層
    10. 針對 Application Insights 資源選取 [暫時略過]
    11. 出現提示時,按一下 [部署]
    12. 選取 [MyFirstAzureWebApp] 作為要部署的資料夾。
    13. 出現提示時,請選取 [新增設定]
  4. 在快顯 [一律將工作區 "MyFirstAzureWebApp" 部署至 <app-name>] 中,選取 [是],以便每次您在該工作區中時,Visual Studio Code 都會部署至相同的App Service應用程式。

  5. 發佈完成時,請在通知中選取 [Browse Website] \(瀏覽網站\),然後在收到提示時選取 [開啟]

    頁面會顯示 ASP.NET Core 7.0 Web 應用程式。

    Screenshot of Visual Studio Code - ASP.NET Core 7.0 web app in Azure.

  1. 使用 az login 命令登入您的 Azure 帳戶,並遵循下列提示:

    az login
    
    • 若無法辨識 az 命令,請確定您已安裝 Azure CLI,如先決條件中所述。
  2. 直接使用 az webapp up 命令,在您的本機 MyFirstAzureWebApp 目錄中部署程式碼:

    az webapp up --sku F1 --name <app-name> --os-type <os>
    
    • 以 Azure 中的唯一名稱 (有效字元為 a-z0-9-) 取代 <app-name>。 良好的模式是使用您的公司名稱和應用程式識別碼的組合。
    • --sku F1 引數會在 [免費]定價層上建立 Web 應用程式。 省略這個引數並使用進階層,如此會產生每小時成本。
    • linuxwindows 取代 <os>
    • 您可以選擇性地加入引數 --location <location-name>,其中 <location-name> 是可用的 Azure 區域。 您可執行 az account list-locations 命令,擷取 Azure 帳戶的允許區域清單。

    此命令可能需要幾分鐘才能完成。 執行時,此命令會提供建立資源群組、App Service 方案、裝載應用程式、設定記錄,然後執行 ZIP 部署的相關訊息。 然後,其會顯示具有應用程式 URL 的訊息:

    You can launch the app at http://<app-name>.azurewebsites.net
    
  3. 開啟網頁瀏覽器,然後巡覽至 URL:

    頁面會顯示 ASP.NET Core 7.0 Web 應用程式。

    Screenshot of the CLI - ASP.NET Core 7.0 web app in Azure.

注意

針對在 Windows 裝載平台上建立應用程式,建議使用 Azure PowerShell。 若要在 Linux 上建立應用程式,請使用不同的工具,例如 Azure CLI

  1. 使用 Connect-AzAccount 命令登入您的 Azure 帳戶,並遵循下列提示:

    Connect-AzAccount
    
  1. 使用 New-AzWebApp 命令建立新的應用程式:

    New-AzWebApp -Name <app-name> -Location westeurope
    
    • 以 Azure 中的唯一名稱 (有效字元為 a-z0-9-) 取代 <app-name>。 貴公司名稱和應用程式識別碼的組合是良好的模式。
    • 您可以選擇性地包含參數 -Location <location-name>,其中 <location-name> 是可用的 Azure 區域。 您可執行 Get-AzLocation 命令,擷取 Azure 帳戶的允許區域清單。

    此命令可能需要幾分鐘才能完成。 執行時,此命令會建立資源群組、App Service 方案與 App Service 資源。

  2. 從應用程式根資料夾中,使用 dotnet publish 命令準備本機 MyFirstAzureWebApp 應用程式以進行部署:

    dotnet publish --configuration Release
    
  3. 變更為發行目錄,並從內容建立 zip 檔案:

    cd bin\Release\net7.0\publish
    Compress-Archive -Path * -DestinationPath deploy.zip
    
  4. 使用 Publish-AzWebApp 命令將 zip 檔案發佈至 Azure 應用程式:

    Publish-AzWebApp -ResourceGroupName myResourceGroup -Name <app-name> -ArchivePath (Get-Item .\deploy.zip).FullName -Force
    

    注意

    -ArchivePath 需要 zip 檔案的完整路徑。

  5. 開啟網頁瀏覽器,然後巡覽至 URL:

    頁面會顯示 ASP.NET Core 7.0 Web 應用程式。

    Screenshot of the CLI - ASP.NET Core 7.0 web app in Azure.

  1. 在搜尋中輸入應用程式服務。 在 [服務] 下,選取 [App Service]

    Screenshot of portal search in the Azure portal.

  2. 在 [App Service] 頁面中,選取 [+ 建立]

  3. 在 [基本] 索引標籤中:

    • 在 [資源群組] 下,選取 [新建]。 輸入 myResourceGroup 作為名稱。
    • 在 [名稱] 下,輸入 Web 應用程式的全域唯一名稱。
    • 在 [發佈] 下,選取 [程式碼]
    • 在 [執行階段堆疊] 下,選取 [.NET 7 (STS)]
    • 選取 [作業系統],以及您想要從中提供應用程式的 [區域]
    • 在 [App Service 方案] 下方,選取 [新建],然後輸入 [myAppServicePlan] 作為名稱。
    • 在 [定價方案] 下方選取 [免費 F1]

    Screenshot of new App Service app configuration for .NET 7 in the Azure portal.

  4. 選取頁面頂端的 [部署] 索引標籤

  5. 在 [GitHub Actions 設定] 下方,將 [持續部署] 設為 [啟用]

  6. 在 [GitHub Actions 詳細資料] 下,使用您的 GitHub 帳戶進行驗證,然後選取下列選項:

    • 針對 [組織],選取您已在其中派生示範專案的組織。
    • 針對 [存放庫], 選取 dotnetcore-docs-hello-world 專案。
    • 針對 [分支],選取 [主要]

    Screenshot of the deployment options for an app using the .NET 6 runtime.

    注意

    根據預設,建立精靈會停用基本驗證,並使用使用者指派的身分識別建立 GitHub Actions 部署。 如果您在資源建立期間收到權限錯誤,則您的 Azure 帳戶可能沒有足夠的權限。 您可以稍後使用 Azure 系統管理員為您產生的身分識別來設定 GitHub Actions 部署,也可以改為啟用基本驗證。

  7. 選取頁面底部的 [檢閱 + 建立] 按鈕。

  8. 驗證執行之後,請選取頁面底部的 [建立] 按鈕。

  9. 當部署完成時,選取 [前往資源]

    Screenshot of the next step of going to the resource.

  10. 使用網頁瀏覽器瀏覽至已部署的應用程式 (URL 為 http://<app-name>.azurewebsites.net)。

  1. 使用 az login 命令登入您的 Azure 帳戶,並遵循下列提示:

    azd auth login
    
  2. 使用 azd up 命令建立 Azure 資源並部署您的應用程式:

    azd up
    

    azd up 命令可能需要幾分鐘才能完成。 azd up 會使用專案中的 Bicep 檔案來建立資源群組、App Service 方案及裝載應用程式。 它也會執行某些設定,例如啟用記錄並部署已編譯的應用程式程式碼。 執行時,此命令會提供佈建和部署程序的相關資訊,包括 Azure 中部署的連結。 完成時,此命令也會顯示部署應用程式的連結。

  3. 開啟網頁瀏覽器,然後巡覽至 URL:

    頁面會顯示 ASP.NET Core 7.0 Web 應用程式。

    Screenshot of the deployed .NET Framework 4.8 sample app.

3.更新應用程式並重新部署

請遵循下列步驟來更新和重新部署您的 Web 應用程式:

  1. 在 [方案總管] 的專案下,開啟 Index.cshtml

  2. 使用下列程式碼取代第一個 <div> 項目:

    <div class="jumbotron">
        <h1>.NET 💜 Azure</h1>
        <p class="lead">Example .NET app to Azure App Service.</p>
    </div>
    

    儲存您的變更。

  3. 若要重新部署至 Azure,請在 [方案總管] 中,以滑鼠右鍵按一下 MyFirstAzureWebApp 專案,然後選取 [發佈]

  4. 在 [發佈] 摘要頁面中,選取 [發佈]

    發佈完成時,Visual Studio 會啟動瀏覽器以前往 Web 應用程式的 URL。

    頁面會顯示已更新的 ASP.NET Core 7.0 Web 應用程式。

    Screenshot of Visual Studio - Updated ASP.NET Core 7.0 web app in Azure.

  1. 編輯 Index.cshtml

  2. 使用下列程式碼取代第一個 <div> 項目:

    <div class="jumbotron">
        <h1>.NET 💜 Azure</h1>
        <p class="lead">Example .NET app to Azure App Service.</p>
    </div>
    

    儲存您的變更。

  3. 在 Visual Studio Code 中,開啟命令選擇區Ctrl+Shift+P

  4. 搜尋並選取 [Azure App Service: Deploy to Web App] \(Azure App Service:部署至 Web 應用程式\)。

  5. 收到提示時,請選取 [部署]

  6. 發佈完成時,請在通知中選取 [Browse Website] \(瀏覽網站\),然後在收到提示時選取 [開啟]

    頁面會顯示已更新的 ASP.NET Core 7.0 Web 應用程式。

    Screenshot of Visual Studio Code - Updated ASP.NET Core 7.0 web app in Azure.

在本機目錄中,開啟 Index.cshtml 檔案。 取代第一個 <div> 項目:

<div class="jumbotron">
    <h1>.NET 💜 Azure</h1>
    <p class="lead">Example .NET app to Azure App Service.</p>
</div>

儲存您的變更,然後再次使用 az webapp up 命令重新部署應用程式:

ASP.NET Core 7.0 是跨平台服務,會根據您先前的部署,以 linuxwindows 取代 <os>

az webapp up --os-type <os>

此命令會使用在 .azure/config 檔案中本機快取的值,包括應用程式名稱、資源群組和 App Service 方案。

部署完成後,切換回在「瀏覽至應用程式」步驟中開啟的瀏覽器視窗,然後按 [重新整理]。

頁面會顯示已更新的 ASP.NET Core 7.0 Web 應用程式。

Screenshot of the CLI - Updated ASP.NET Core 7.0 web app in Azure.

  1. 在本機目錄中,開啟 Index.cshtml 檔案。 取代第一個 <div> 項目:

    <div class="jumbotron">
        <h1>.NET 💜 Azure</h1>
        <p class="lead">Example .NET app to Azure App Service.</p>
    </div>
    
  2. 從應用程式根資料夾中,使用 dotnet publish 命令準備本機 MyFirstAzureWebApp 應用程式以進行部署:

    dotnet publish --configuration Release
    
  3. 變更為發行目錄,並從內容建立 zip 檔案:

    cd bin\Release\net7.0\publish
    Compress-Archive -Path * -DestinationPath deploy.zip
    
  4. 使用 Publish-AzWebApp 命令將 zip 檔案發佈至 Azure 應用程式:

    Publish-AzWebApp -ResourceGroupName myResourceGroup -Name <app-name> -ArchivePath (Get-Item .\deploy.zip).FullName -Force
    

    注意

    -ArchivePath 需要 zip 檔案的完整路徑。

  5. 部署完成後,切換回在「瀏覽至應用程式」步驟中開啟的瀏覽器視窗,然後按 [重新整理]。

    頁面會顯示已更新的 ASP.NET Core 7.0 Web 應用程式。

    Screenshot of the CLI - Updated ASP.NET Core 7.0 web app in Azure.

  1. 瀏覽至範例程式碼的 GitHub 分支。

  2. 在存放庫頁面上,按下 . 來在瀏覽器中啟動 Visual Studio Code。

    注意

    URL 會從 GitHub.com 改為 GitHub.dev。 此功能僅適用於擁有檔案的存放庫。 而不適用於空的存放庫。

  3. 編輯 Index.cshtml

    Index.cshtml 位於 Pages 資料夾中。

    Screenshot of the Explorer window from Visual Studio Code in the browser, highlighting the Index.cshtml in the dotnetcore-docs-hello-world repo.

  4. 使用下列程式碼取代第一個 <div> 項目:

    <div class="jumbotron">
        <h1>.NET 💜 Azure</h1>
        <p class="lead">Example .NET app to Azure App Service.</p>
    </div>
    

    儲存您的變更。

  5. 從 [原始檔控制] 功能表中,選取 [階段變更] 按鈕以暫存變更。

  6. 輸入認可訊息,例如 We love Azure。 然後,選取 [認可] 及 [推送]

  7. 部署完成後,返回在瀏覽至應用程式步驟中開啟的瀏覽器視窗,然後重新整理頁面。

    頁面會顯示已更新的 ASP.NET Core 7.0 Web 應用程式。

    Screenshot of the CLI - Updated ASP.NET Core 7.0 web app in Azure.

在本機目錄中,開啟 Index.cshtml 檔案。 取代第一個 <div> 項目:

<div class="jumbotron">
    <h1>.NET 💜 Azure</h1>
    <p class="lead">Example .NET app to Azure App Service.</p>
</div>

儲存您的變更,然後再次使用 azd up 命令重新部署應用程式:

azd up

這次 azd up 會略過佈建資源步驟,而且只會重新部署您的程式碼,因為 Bicep 檔案沒有變更。

部署完成後,瀏覽器會開啟至已更新的 ASP.NET Core 7.0 Web 應用程式。

Screenshot of the CLI - Updated ASP.NET Core 7.0 web app in Azure.

4.管理 Azure 應用程式

若要管理 Web 應用程式,請移至 Azure 入口網站,然後搜尋並選取 [應用程式服務]

Screenshot of the Azure portal - Select App Services option.

在 [應用程式服務] 頁面上,選取您的 Web 應用程式名稱。

Screenshot of the Azure portal - App Services page with an example web app selected.

Web 應用程式的概觀頁面,其中包含瀏覽、停止、啟動、重新啟動和刪除等基本管理選項。 左側功能表提供的頁面可用來進一步設定您的應用程式。

Screenshot of the Azure portal - App Service overview page.

清除資源

在上述步驟中,您已建立資源群組中的 Azure 資源。 如果您預期未來不需要這些資源,則可以藉由刪除資源群組予以刪除。

  1. 從 Azure 入口網站中的 Web 應用程式概觀頁面,選取資源群組底下的 myResourceGroup 連結。
  2. 在 [資源群組] 頁面上,確定所列出資源是您想要刪除的資源。
  3. 選取 [刪除],在文字方塊中鍵入 ,然後選取 [刪除]

清除資源

在上述步驟中,您已建立資源群組中的 Azure 資源。 如果您預期未來不需要這些資源,則可以藉由刪除資源群組予以刪除。

  1. 從 Azure 入口網站中的 Web 應用程式概觀頁面,選取資源群組底下的 myResourceGroup 連結。
  2. 在 [資源群組] 頁面上,確定所列出資源是您想要刪除的資源。
  3. 選取 [刪除],在文字方塊中鍵入 ,然後選取 [刪除]

清除資源

在上述步驟中,您已建立資源群組中的 Azure 資源。 如果您在未來不需要這些資源,請在 Cloud Shell 中執行下列命令,刪除資源群組:

az group delete --name myResourceGroup

此命令可能會花一分鐘執行。

清除資源

在上述步驟中,您已建立資源群組中的 Azure 資源。 若您預期未來不需要使用這些資源,請執行下列 PowerShell 命令來刪除資源群組:

Remove-AzResourceGroup -Name myResourceGroup

此命令可能會花一分鐘執行。

清除資源

在上述步驟中,您已建立資源群組中的 Azure 資源。 如果您預期未來不需要這些資源,則可以藉由刪除資源群組予以刪除。

  1. 從 Azure 入口網站中的 Web 應用程式概觀頁面,選取資源群組底下的 myResourceGroup 連結。
  2. 在 [資源群組] 頁面上,確定所列出資源是您想要刪除的資源。
  3. 選取 [刪除],在文字方塊中鍵入 ,然後選取 [刪除]

清除資源

使用 azd down 命令來移除資源群組及其所有相關聯的資源:

azd down

如需使用 azd 範本和其他功能的後續步驟,請參閱 Azure Developer CLI 文件。

下一步