使用 Visual Studio 的工具部署 ASP.NET 應用程式

本頁面說明如何設定 Microsoft Visual Studio,以及如何在 Google Cloud上建立及部署 ASP.NET 應用程式。

事前準備

  1. In the Google Cloud console, on the project selector page, click Create project to begin creating a new Google Cloud project.

    Go to project selector

  2. Make sure that billing is enabled for your Google Cloud project.

  3. 確認您的本機系統已安裝下列軟體:
  4. 建立新的 Windows VM 來託管您的應用程式:
    1. 前往 Google Cloud Marketplace 上的 ASP.NET Framework 映像檔。
      前往 ASP.NET Framework 映像檔
    2. 選取 [Launch on Compute Engine] (在 Compute Engine 上啟動)。
    3. 選擇部署名稱和設定,確實勾選「Allow HTTP Traffic」(允許 HTTP 流量) 和「Allow WebDeploy Traffic」(允許 WebDeploy 流量)。
    4. 選取「Deploy」

安裝 Cloud Tools for Visual Studio

如何透過 Visual Studio 中的「Extensions and Updates」(擴充功能和更新) 對話方塊安裝 Cloud Tools for Visual Studio:

  1. 依序選取 [Tools] (工具) > [Extensions and Updates] (擴充功能和更新)。
  2. 選取左側窗格中的 [Online] (線上) 商情項目
  3. 搜尋「Google Cloud Tools for Visual Studio」。
  4. 點選 [Download] (下載) 並按照提示操作。
  5. 重新啟動 Visual Studio,以載入擴充功能。

登入 Google

登入您用來將專案部署到 App Engine 的 Google 帳戶。

  1. 開啟 Visual Studio。
  2. 啟動 Cloud Explorer,方法是依序點選 [Tools] (工具) > [Google Cloud Tools] > [Show Google Cloud Explorer] (顯示 Google Cloud Explorer)。
  3. 按一下 [Select or create account] (選取或建立帳戶)。
  4. 按一下「新增帳戶」
  5. 系統會隨即開啟新的瀏覽器視窗,並要求您授予管理 Google Cloud 資源所需的權限。

  6. 為應用程式選取要使用的帳戶。
  7. 按一下 [Allow] (允許),然後關閉瀏覽器視窗。

選取專案

如何選取要部署應用程式的 Google Cloud 專案:

  1. Cloud Explorer 中按一下 [Select Project] (選取專案)。
  2. 在清單中選取所需專案。
  3. 按一下 [OK] (確定)

Cloud Explorer 會隨即重新整理並針對所選專案顯示資源。

建立 ASP.NET 應用程式

如何建立新的 ASP.NET 專案並在本機執行:

  1. 依序選擇 [File] (檔案) > [New] (新增) > [Project] (專案) 以開啟「New Project」(新增專案) 對話方塊。
  2. 使用左側面板依序前往 [Installed] (已安裝) > [Visual C#] > [Google Cloud Platform]。
  3. 選取 [ASP.NET on Google Cloud Platform] (Google Cloud Platform 上的 ASP.NET)。
  4. 如要將應用程式部署至 Compute Engine 上的 VM,請指定 .NET Framework,而非 .NET Core。針對 GKE 或 App Engine,請使用 [ASP.NET Core on Google Cloud Platform] (Google Cloud Platform 上的 ASP.NET)

  5. 為您的專案輸入名稱
  6. 按一下 [OK] (確定)
  7. 選取 [Template type] (範本類型)。
  8. 按一下 [OK] (確定)
  9. 建構解決方案並在本機執行,確認運作正常。

部署 ASP.NET 應用程式

如何將您的 ASP.NET 應用程式部署到在 Compute Engine 上執行的 Windows VM:

  1. 在 Cloud Explorer 中,選取新建的專案並展開 GKE。
  2. 以滑鼠右鍵按一下您在上述步驟中使用 Cloud Marketplace 建立的 VM 執行個體。
  3. 選取「管理 Windows 憑證」
  4. 按一下 [Add credentials] (新增憑證)
  5. 輸入使用者名稱。
  6. 確實選取 [Create a password for me] (為我建立密碼),然後按一下 [Save] (儲存)。
  7. 系統顯示「Reset the password for [your username] on the instance [instance you created]?」(要在「<您建立的執行個體>」執行個體為 <您的使用者名稱> 重設密碼嗎?) 確認問題時,按一下「Reset」(重設)
  8. 按一下 [Close] (關閉) 按鈕,以關閉密碼視窗。
  9. 點選 [Close] (關閉) 按鈕,以關閉「Windows Credentials」(Windows 憑證) 視窗。
  10. 在 Visual Studio 的「Solution Explorer」(解決方案瀏覽器) 窗格中,以滑鼠右鍵按一下所需應用程式。
  11. 在內容選單中,按一下「Publish to Google Cloud」
  12. 在「Publish」對話方塊中,選擇「Compute Engine」
  13. 按一下「Publish」即可建立及部署範例應用程式。 發布完成後,Visual Studio 會在您的預設網路瀏覽器中開啟應用程式:

大功告成,您的應用程式已於 Compute Engine 託管。

後續步驟