gpt4 book ai didi

c# - 将数组作为命令行参数传递给 Asp.Net Core

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:26 25 4
gpt4 key购买 nike

需要将数组作为命令行参数传递给 Asp.Net Core 托管服务。

添加的提供者

config
.AddJsonFile("appsettings.json")
.AddCommandLine(args);

我在应用程序的某个地方

var actions = _configuration.GetSection("actions").Get<List<string>>();
foreach (var action in actions)
{
Console.WriteLine(action);
}

尝试像这样运行应用

dotnet MyService.dll --actions Action1,Action2
dotnet MyService.dll --actions [Action1,Action2]
dotnet MyService.dll --actions ["Action1","Action2"]

但没有结果,actionsnull

当我添加 "actions": ["Action1","Action2"] 时到 appsettings.json 然后绑定(bind)效果很好。

如何将数组作为命令行参数传递?

我可以通过这种方式得到它_configuration.GetValue<string>("actions").Split(","); , 但如何将其绑定(bind)到列表?

最佳答案

ASP.NET Core 配置是一组键值对。您显示的 appsettings.json 属性将转换为以下内容:

  • Action :0 = Action 1
  • Action :1 = Action 2

提供这组相同的键值对作为多个命令行参数以获得所需的结果:

dotnet MyService.dll --actions:0 Action1 --actions:1 Action2

关于c# - 将数组作为命令行参数传递给 Asp.Net Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58997602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com