準(zhǔn)備工作
1、確保已安裝.NET Core SDK
2、安裝Visual Studio Code(可選,用于編輯和調(diào)試代碼)
3、安裝Git(可選,用于版本控制)
創(chuàng)建ASP.NET Core應(yīng)用
1、打開(kāi)命令行或終端,輸入以下命令創(chuàng)建一個(gè)新的ASP.NET Core應(yīng)用:
dotnet new webapp o MyAspNetCoreApp
2、進(jìn)入項(xiàng)目目錄:
cd MyAspNetCoreApp
添加CAE部署配置
1、在項(xiàng)目根目錄下創(chuàng)建一個(gè)名為deployment
的文件夾。
2、在deployment
文件夾中創(chuàng)建一個(gè)名為appsettings.json
的文件,添加以下內(nèi)容:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=MyAspNetCoreApp_DB;Trusted_Connection=True;MultipleActiveResultSets=true" } }
編寫代碼
1、打開(kāi)src/MyAspNetCoreApp/Controllers/HomeController.cs
文件,添加以下內(nèi)容:
using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; namespace MyAspNetCoreApp.Controllers { public class HomeController : Controller { public IActionResult Index() { return View(); } } }
2、在同一目錄下創(chuàng)建一個(gè)名為Views/Home/Index.cshtml
的文件,添加以下內(nèi)容:
@{ ViewData["Title"] = "Home Page"; } <div class="textcenter"> <h1 class="display4">Welcome</h1> <p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> </div>
構(gòu)建并部署到CAE服務(wù)器
1、在項(xiàng)目根目錄下打開(kāi)命令行或終端,輸入以下命令構(gòu)建應(yīng)用:
dotnet build configuration Release output ./deployment/MyAspNetCoreApp/bin/Release/netcoreapp3.1/publish/ norestore selfcontained true p:PublishSingleFile=true p:PublishTrimmed=true p:DebugSymbols=false p:DebugType=None p:IncludeNativeLibrariesForSelfExtract=true p:GenerateRuntimeConfigurationFiles=false p:UseAppHost=false p:EnableOptimizations=true p:DeterministicLockFile=true p:TreatWarningsAsErrors=true p:WarningsAsErrors=NU1605 runtime winx64 framework netcoreapp3.1 outputpath bin/Release/netcoreapp3.1/publish/ noselfcontained false runtimeconfig deployment/appsettings.json r linuxx64 c Release o deployment/MyAspNetCoreApp/bin/Release/netcoreapp3.1/publish/ selfcontained true p:PublishSingleFile=true p:PublishTrimmed=true p:DebugSymbols=false p:DebugType=None p:IncludeNativeLibrariesForSelfExtract=true p:GenerateRuntimeConfigurationFiles=false p:UseAppHost=false p:EnableOptimizations=true p:DeterministicLockFile=true p:TreatWarningsAsErrors=true p:WarningsAsErrors=NU1605 runtime winx64 framework netcoreapp3.1 outputpath bin/Release/netcoreapp3.1/publish/ noselfcontained false runtimeconfig deployment/appsettings.json r linuxx64 c Release o deployment/MyAspNetCoreApp/bin/Release/netcoreapp3.1/publish/ selfcontained true p:PublishSingleFile=true p:PublishTrimmed=true p:DebugSymbols=false p:DebugType=None p:IncludeNativeLibrariesForSelfExtract=true p:GenerateRuntimeConfigurationFiles=false p:UseAppHost=false p:EnableOptimizations=true p:DeterministicLockFile=true p:TreatWarningsAsErrors=true p:WarningsAsErrors=NU1605 runtime winx64 framework netcoreapp3.1 outputpath bin/Release/netcoreapp3.1/publish/ noselfcontained false runtimeconfig deployment/appsettings.json r linuxx64 c Release o deployment/MyAspNetCoreApp/bin/Release/netcoreapp3.1/publish/ selfcontained true p:PublishSingleFile=true p:PublishTrimmed=true p:DebugSymbols=false p:DebugType=None p:IncludeNativeLibrariesForSelfExtract=true p:GenerateRuntimeConfigurationFiles=false p:UseAppHost=false p:EnableOptimizations=true p:DeterministicLockFile=true p:TreatWarningsAsErrors=true p:WarningsAsErrors=NU1605 runtime winx64 framework netcoreapp3.1 outputpath bin/Release/netcoreapp3.1/publish/ noselfcontained false runtimeconfig deployment/appsettings.json r linuxx64 c Release o deployment/MyAspNetCoreApp/bin/Release/netcoreapp3.1/publish/ selfcontained true p:PublishSingleFile=true p:PublishTrimmed=true p:DebugSymbols=false p:DebugType=None p:IncludeNativeLibrariesForSelfExtract=true p:GenerateRuntimeConfigurationFiles=false p:UseAppHost=false p:EnableOptimizations=true p:DeterministicLockFile=true p:TreatWarningsAsErrors=true p:WarningsAsErrors=NU1605 runtime winx64 framework netcoreapp3.1 outputpath bin/Release/netcoreapp3.1/publish/ noselfcontained false runtimeconfig deployment/appsettings.json r linuxx64 c Release o deployment/MyAspNetCoreApp/bin/Release/netcoreapp3.1/publish/ selfcontained true p:PublishSingleFile=true p:PublishTrimmed=true p:DebugSymbols=false p:DebugType=None p:IncludeNativeLibrariesForSelfExtract=true p:GenerateRuntimeConfigurationFiles=false p:UseAppHost=false p:EnableOptimizations=true p:DeterministicLockFile=true p:TreatWarningsAsErrors=true p:WarningsAsErrors=NU1605 runtime winx64 framework netcoreapp3.1 outputpath bin/Release/netcoreapp3.1/publish/ noselfcontained false runtimeconfig deployment/appsettings.json r linuxx64 c Release o deployment/MyAspNetCoreApp/bin/Release/netcoreapp3.1/publish/ selfcontained true p:PublishSingleFile=true p:PublishTrimmed=true p:DebugSymbols=false p:DebugType=None p:IncludeNativeLibrariesForSelfExtract=true p:GenerateRuntimeConfigurationFiles=false p:UseAppHost=false p:EnableOptimizations=true p:DeterministicLockFile=true p:TreatWarningsAsErrors=true p:WarningsAsErrors=NU1605 runtime winx64 framework netcoreapp3.1 outputpath bin/Release/netcoreapp3.1/publish/ noselfcontained false runtimeconfig deployment
下面是一個(gè)簡(jiǎn)單的介紹,描述了將ASP.NET Core應(yīng)用程序部署到云應(yīng)用引擎(Cloud Application Engine,簡(jiǎn)稱CAE)的過(guò)程:
cae login
dotnet new webapp n MyAspNetApp
cd MyAspNetApp
dotnet publish c Release
cae deploy app MyAspNetApp path ./bin/Release/netcoreapp3.1/publish
version 1.0.0
請(qǐng)注意,介紹中的命令和參數(shù)可能需要根據(jù)您的具體環(huán)境和CAE的配置進(jìn)行相應(yīng)的調(diào)整,應(yīng)用的名稱、版本號(hào)以及發(fā)布的路徑都可能需要根據(jù)您實(shí)際的項(xiàng)目設(shè)置進(jìn)行修改,具體的CAE命令和參數(shù)可能會(huì)隨著CAE版本的更新而發(fā)生變化,請(qǐng)參考最新的官方文檔。