ASP.NET Core应用部署到CAE
准备工作
1、确保已经安装了.NET Core SDK和Visual Studio。
2、在项目中使用以下命令安装CAE部署工具包:
```
dotnet add package Microsoft.Extensions.Hosting version 3.0.0
```
创建Web服务器项目
1、打开Visual Studio,选择"新建项目"。
2、在模板中选择"Web应用程序(.NET Core)"。
3、输入项目名称和保存位置,点击"确定"。
4、在解决方案资源管理器中右键单击项目,选择"属性"。
5、在"属性页"中,将"启动项目"设置为"Web服务器"。
6、在"Program.cs"文件中,添加以下代码来配置Kestrel服务器:
```csharp
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace WebServerApp
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
```
7、创建一个名为"Startup.cs"的新文件,并添加以下代码来配置应用程序的中间件管道:
```csharp
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace WebServerApp
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
}
}
}
```
8、运行项目,确保没有错误。
9、在浏览器中访问http://localhost:5000
,你应该能看到默认的欢迎页面。
部署到CAE服务器
1、确保你已经获取了CAE服务器的访问权限和IP地址。
2、在项目中使用以下命令发布应用程序:
```shell
dotnet publish c Release o <输出目录> selfcontained true runtime winx64 framework netcoreapp3.0 output <输出目录> deployserverpackage deploysource https://<i><服务器IP地址></i>:5001/MyDeployedApp/ <输出目录>/PublishOutput/winx64/publish/<应用程序名称>.exe /p:UseAppHost=false /p:PublishTrimmed=true /p:DebugSymbols=false /p:DebugType=None /p:DisableOptimizations=false /p:AllowUnsafeBlocks=false /p:GenerateAssemblyInfo=false /p:IncludeNativeLibrariesForSelfExtract=true /p:EnableCompressedFiles=true /p:PublishReadyToRun=true /p:PublishSingleFile=true /p:PublishTrimmed=true /p:RuntimeIdentifier=winx64 /p:TargetFrameworkVersion=v3.0 /p:VersionSuffix=beta1 /p:SelfContained=true /p:ZipAfterPublish=true /p:_PackageTempDir=%temp%\8dbf9f4ea3b5452d9e7d5797e6c3e3a9 %USERPROFILE%\Desktop\<caedeploy\deployscript\deploytocae.ps1 r winx64 c Release o <输出目录> selfcontained true runtime winx64 framework netcoreapp3.0 output <输出目录> deployserverpackage deploysource https://<i><服务器IP地址></i>:5001/MyDeployedApp/ <输出目录>/PublishOutput/winx64/publish/<应用程序名称>.exe /p:UseAppHost=false /p:PublishTrimmed=true /p:DebugSymbols=false /p:DebugType=None /p:DisableOptimizations=false /p:AllowUnsafeBlocks=false /p:GenerateAssemblyInfo=false /p:IncludeNativeLibrariesForSelfExtract=true /p:EnableCompressedFiles=true /p:PublishReadyToRun=true /p:PublishSingleFile=true /p:PublishTrimmed=true /p:RuntimeIdentifier=winx64 /p:TargetFrameworkVersion=v3.0 /p:VersionSuffix=beta1 /p:SelfContained=true /p:ZipAfterPublish=true /p:_PackageTempDir=%temp%\8dbf9f4ea3b5452d9e7d5797e6c3e3a9 %USERPROFILE%\Desktop\caedeploy\deployscript\deploytocae.ps1 r winx64 c Release o <输出目录> selfcontained true runtime winx64 framework netcoreapp3.0 output <输出目录> deployserverpackage deploysource https://<br><span class="cursor cursor0"></span>
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/525073.html