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