.NET core Pass Commandline Args to Startup.cs from Program.cs

前端 未结 3 520
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 22:44

I\'m trying to configure kestrel so that when it\'s in it\'s raw mode it runs on a specific port. However to do so it appears that the launchsettings.json needs to pass comm

3条回答
  •  [愿得一人]
    2021-01-04 23:19

    A simple solution is to access the command line arguments through the Environment.GetCommandLineArgs method.

    You only need to make sure that you remove the first argument, which is the executable name:

    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var args = Environment.GetCommandLineArgs().Skip(1).ToArray();
            var builder = new ConfigurationBuilder();
            builder.AddCommandLine(args);
    
            Configuration = builder.Build();
        }
    }
    

提交回复
热议问题