我正在使用来自不同目录(不是 dll 目录)的脚本启动 ASP.NET Core API
问题是默认的内容根路径是脚本的目录。
如何将其更改为dll的目录?
仅当从 dll 目录启动脚本时才有效
可以通过两种方式修复:
1 - 来自 Program.cs
您可以对 Program.cs 中的内容根进行硬编码以映射 dll 的目录:
public Static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
...
.UseContentRoot(AppContext.BaseDirectory)
...
}
2 - 将其设置为环境变量
变量名称:{PREFIX_}CONTENTROOT(前缀是 DOTNET 或 ASPNETCORE)
export ASPNETCORE_CONTENTROOT=$HOME/myapi
我遇到了同样的问题 - 我的 .NET 8 应用程序是由另一个应用程序启动的,因此当前工作目录不是我的应用程序的启动文件夹。为了解决这个问题,我使用了:
WebApplicationOptions options = new() {
ContentRootPath = Application.StartupPath
};
WebApplicationBuilder webAppBuilder = webApplication.CreateBuilder(options);