繁体   English   中英

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

[英]Pass array as a command line argument to Asp.Net Core

需要将数组作为命令行参数传递给 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 时,绑定效果很好。

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

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

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

  • 行动:0 = 行动1
  • 行动:1 = 行动2

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM