使用 Visual Studio 的工具部署 ASP.NET 應用程式
本頁面說明如何設定 Microsoft Visual Studio,以及如何在 Google Cloud上建立及部署 ASP.NET 應用程式。
事前準備
-
In the Google Cloud console, on the project selector page, click Create project to begin creating a new Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
- 確認您的本機系統已安裝下列軟體:
- Visual Studio 2022、Visual Studio 2019 或 Visual Studio 2017,任何 Windows 版本。Cloud Tools for Visual Studio 不支援 Visual Studio 2017 Mac 版。
- gcloud CLI
- 建立新的 Windows VM 來託管您的應用程式:
- 前往 Google Cloud Marketplace 上的 ASP.NET Framework 映像檔。
前往 ASP.NET Framework 映像檔 - 選取 [Launch on Compute Engine] (在 Compute Engine 上啟動)。
- 選擇部署名稱和設定,確實勾選「Allow HTTP Traffic」(允許 HTTP 流量) 和「Allow WebDeploy Traffic」(允許 WebDeploy 流量)。
- 選取「Deploy」。
安裝 Cloud Tools for Visual Studio
如何透過 Visual Studio 中的「Extensions and Updates」(擴充功能和更新) 對話方塊安裝 Cloud Tools for Visual Studio:
- 依序選取 [Tools] (工具) > [Extensions and Updates] (擴充功能和更新)。
- 選取左側窗格中的 [Online] (線上) 商情項目。
- 搜尋「Google Cloud Tools for Visual Studio」。
- 點選 [Download] (下載) 並按照提示操作。
- 重新啟動 Visual Studio,以載入擴充功能。
登入 Google
登入您用來將專案部署到 App Engine 的 Google 帳戶。
- 開啟 Visual Studio。
- 啟動 Cloud Explorer,方法是依序點選 [Tools] (工具) > [Google Cloud Tools] > [Show Google Cloud Explorer] (顯示 Google Cloud Explorer)。
- 按一下 [Select or create account] (選取或建立帳戶)。
- 按一下「新增帳戶」。
- 為應用程式選取要使用的帳戶。
- 按一下 [Allow] (允許),然後關閉瀏覽器視窗。
系統會隨即開啟新的瀏覽器視窗,並要求您授予管理 Google Cloud 資源所需的權限。
選取專案
如何選取要部署應用程式的 Google Cloud 專案:
- 在 Cloud Explorer 中按一下 [Select Project] (選取專案)。
- 在清單中選取所需專案。
- 按一下 [OK] (確定)。
Cloud Explorer 會隨即重新整理並針對所選專案顯示資源。
建立 ASP.NET 應用程式
如何建立新的 ASP.NET 專案並在本機執行:
- 依序選擇 [File] (檔案) > [New] (新增) > [Project] (專案) 以開啟「New Project」(新增專案) 對話方塊。
- 使用左側面板依序前往 [Installed] (已安裝) > [Visual C#] > [Google Cloud Platform]。
- 選取 [ASP.NET on Google Cloud Platform] (Google Cloud Platform 上的 ASP.NET)。
- 為您的專案輸入名稱。
- 按一下 [OK] (確定)。
- 選取 [Template type] (範本類型)。
- 按一下 [OK] (確定)。
- 建構解決方案並在本機執行,確認運作正常。
如要將應用程式部署至 Compute Engine 上的 VM,請指定 .NET Framework,而非 .NET Core。針對 GKE 或 App Engine,請使用 [ASP.NET Core on Google Cloud Platform] (Google Cloud Platform 上的 ASP.NET)。
部署 ASP.NET 應用程式
如何將您的 ASP.NET 應用程式部署到在 Compute Engine 上執行的 Windows VM:
- 在 Cloud Explorer 中,選取新建的專案並展開 GKE。
- 以滑鼠右鍵按一下您在上述步驟中使用 Cloud Marketplace 建立的 VM 執行個體。
- 選取「管理 Windows 憑證」。
- 按一下 [Add credentials] (新增憑證)。
- 輸入使用者名稱。
- 確實選取 [Create a password for me] (為我建立密碼),然後按一下 [Save] (儲存)。
- 系統顯示「Reset the password for [your username] on the instance [instance you created]?」(要在「<您建立的執行個體>」執行個體為 <您的使用者名稱> 重設密碼嗎?) 確認問題時,按一下「Reset」(重設)
- 按一下 [Close] (關閉) 按鈕,以關閉密碼視窗。
- 點選 [Close] (關閉) 按鈕,以關閉「Windows Credentials」(Windows 憑證) 視窗。
- 在 Visual Studio 的「Solution Explorer」(解決方案瀏覽器) 窗格中,以滑鼠右鍵按一下所需應用程式。
- 在內容選單中,按一下「Publish to Google Cloud」。
- 在「Publish」對話方塊中,選擇「Compute Engine」。
- 按一下「Publish」即可建立及部署範例應用程式。 發布完成後,Visual Studio 會在您的預設網路瀏覽器中開啟應用程式:
大功告成,您的應用程式已於 Compute Engine 託管。
後續步驟
- 進一步瞭解如何瀏覽 Compute Engine 資源。