Rom*_*syk 8 c# command-line-arguments .net-core asp.net-core
需要将数组作为命令行参数传递给 Asp.Net Core 托管服务。
添加的提供者
config
.AddJsonFile("appsettings.json")
.AddCommandLine(args);
Run Code Online (Sandbox Code Playgroud)
我在应用程序的某个地方
var actions = _configuration.GetSection("actions").Get<List<string>>();
foreach (var action in actions)
{
Console.WriteLine(action);
}
Run Code Online (Sandbox Code Playgroud)
尝试运行应用程序
dotnet MyService.dll --actions Action1,Action2
dotnet MyService.dll --actions [Action1,Action2]
dotnet MyService.dll --actions ["Action1","Action2"]
Run Code Online (Sandbox Code Playgroud)
但没有结果,actions就是null
当我添加"actions": ["Action1","Action2"]到 appsettings.json 时,绑定效果很好。
如何将数组作为命令行参数传递?
我可以通过这种方式获得它_configuration.GetValue<string>("actions").Split(",");,但是如何将其绑定到列表?
ASP.NET Core 配置是一组键值对。您显示的 appsettings.json 属性将转换为以下内容:
提供这组相同的键值对作为多个命令行参数以获得所需的结果:
dotnet MyService.dll --actions:0 Action1 --actions:1 Action2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1659 次 |
| 最近记录: |