[英]Access file path from appsettings.json for docker linux
在我的 asp .net 核心 web 应用程序中,我从appsettings.json
访问一些文件路径。 并在那里创建一个包含一些内容的新文件。 以下是我的appsettings.json
:
{
"root_privatekeylocation": "C:\\CARepository\\Root\\rootprivatekey.pem",
"root_publickeylocation": "C:\\CARepository\\Root\\rootpublickey.pem",
"crl_location": "C:\\CARepository\\CRL\\Crl.crl",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
这适用于 windows,因为它创建目录CARepository
、 Root
和文件rootprivatekey.pem
。 以下是创建这些的代码:
string pathFromConfig= Configuration["root_privatekeylocation"];
string privateKey = textWriter.ToString();
//if directory not exist then create
System.IO.FileInfo file = new System.IO.FileInfo(pathFromConfig);
file.Directory.Create(); // If the directory already exists, this method does nothing.
using StreamWriter f = new StreamWriter(pathFromConfig);
f.Write(privateKey);
现在,我需要在 Linux 服务器上运行此应用程序。 So, I add docker support to the application from VS for Linux, create a docker image and run the docker container with this image. 为此,我将 appsettings.json 更改为appsettings.json
将没有C:\\
:
{
"root_privatekeylocation": "~/CARepository/Root/rootprivatekey.pem",
"root_publickeylocation": "~/CARepository/Root/rootpublickey.pem",
"crl_location": "~/CARepository/CRL/Crl.crl",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
调用应用程序 API 在给定路径上创建文件,它返回 output 但没有创建文件~/CARepository/Root/rootprivatekey.pem
我也无法在任何地方找到CARepository
。 似乎它没有创建这条路径。 我签入了 docker 容器 CLI。 有谁能够帮我? Linux docker 是否还有其他文件路径格式,或者我需要更改 C# 代码吗?
不要使用 ~ 来指定用户的主文件夹,使用Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
或选择其他 SpecialFolder 枚举之一。
https://docs.microsoft.com/en-us/dotnet/api/system.environment.getfolderpath?view=net-5.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.