2

ASP.NET Core MVC 2.1

In the documentation here I see CreateDefaultBuilder(string[] args) method. But I don't see any examples its using. Which arguments are expected by the method at this case? I can pass such args: "Hello", "World" but this is unlikely to be what was expected by the method...

Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
  • documentations states `args: The command line args.` – Nkosi May 06 '18 at 09:45
  • @Nkosi I've read it before I created the topic. This information does not help me. – Andrey Bushman May 06 '18 at 09:47
  • 2
    [This article might help](http://www.koderdojo.com/blog/set-host-environment-in-asp-net-core-using-command-line-arguments) –  May 06 '18 at 09:54
  • 2
    The following article should also help [ASP.NET Core – passing command-line arguments to Startup class](https://tpodolak.com/blog/2018/01/16/asp-net-core-passing-command-line-arguments-startup-class/) – Nkosi May 06 '18 at 09:58

1 Answers1

2

Command line arguments are usually used to pass some configuration information to the program when executing it.

A typical program calls CreateDefaultBuilder to start setting up a host.

Any configuration loaded may be overridden by command-line arguments.

For example, the following was taken directly from documentation with some or my own wording

Assuming a hosting.json file like this

{
    urls: "http://*:5005"
}

Overriding the configuration provided by UseUrls with hosting.json config first, command-line argument config second:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("hosting.json", optional: true)
            .AddCommandLine(args) //<--
            .Build();

        return WebHost.CreateDefaultBuilder(args)
            .UseUrls("http://*:5000") //<--
            .UseConfiguration(config) //<--
            .Configure(app =>
            {
                app.Run(context => 
                    context.Response.WriteAsync("Hello, World!"));
            })
            .Build();
    }
}

To specify the host run on a particular URL, the desired value can be passed in from a command prompt when executing dotnet run. The command-line argument overrides the urls value from the hosting.json file, and the server listens on port 8080:

dotnet run --urls "http://*:8080"

Reference Hosting in ASP.NET Core

The following article should also help

ASP.NET Core – passing command-line arguments to Startup class

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Thank you. Is it possible to use it for initializing of *the custom nested settings* are saved in the `appsettings.json` file? For example, can I reset the `stuff` value of such configuration: `{"foo":{"stuff":true}}`? – Andrey Bushman May 06 '18 at 10:40
  • @AndreyBushman yes, https://stackoverflow.com/questions/45219949/pass-command-line-arguments-to-startup-class-in-asp-core – Nkosi May 06 '18 at 11:24