本文介绍了如何将ASP.NET Core应用部署到CAE(云原生应用引擎)。包括了创建CAE集群、配置ASP.NET Core应用和部署应用的详细步骤。
准备工作
1、确保已安装.NET Core SDK
2、安装Visual Studio Code(可选,用于编辑和调试代码)
3、安装Git(可选,用于版本控制)
创建ASP.NET Core应用
1、打开命令行或终端,输入以下命令创建一个新的ASP.NET Core应用:
dotnet new webapp o MyAspNetCoreApp
2、进入项目目录:
cd MyAspNetCoreApp
添加CAE部署配置
1、在项目根目录下创建一个名为deployment
的文件夹。
2、在deployment
文件夹中创建一个名为appsettings.json
的文件,添加以下内容:
{ "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、打开src/MyAspNetCoreApp/Controllers/HomeController.cs
文件,添加以下内容:
using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; namespace MyAspNetCoreApp.Controllers { public class HomeController : Controller { public IActionResult Index() { return View(); } } }
2、在同一目录下创建一个名为Views/Home/Index.cshtml
的文件,添加以下内容:
@{ 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>
构建并部署到CAE服务器
1、在项目根目录下打开命令行或终端,输入以下命令构建应用:
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
下面是一个简单的介绍,描述了将ASP.NET Core应用程序部署到云应用引擎(Cloud Application Engine,简称CAE)的过程:
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
请注意,介绍中的命令和参数可能需要根据您的具体环境和CAE的配置进行相应的调整,应用的名称、版本号以及发布的路径都可能需要根据您实际的项目设置进行修改,具体的CAE命令和参数可能会随着CAE版本的更新而发生变化,请参考最新的官方文档。
本文来源于互联网,如若侵权,请联系管理员删除,本文链接:https://www.9969.net/8970.html