How to start a ASP.NET Core 1.0 RC2 app that doesn

2019-02-24 18:33发布

问题:

how can I start the ASP.NET Core with the dotnet CLI samples so that they don't listen to the localhost?

This command doesn't work:

dotnet run --server.urls=http://*:5000

回答1:

What you're trying to do requires you to add command-line args to your configuration in the Main method of your application. Add something like this before you create your WebHostBuilder object:

var config = new ConfigurationBuilder()
    .AddCommandLine(args)
    .Build();

And then add this to the WebHostBuilder object before calling .Build() on it:

.UseConfiguration(config)

You'll also need to add a dependency to project.json:

"Microsoft.Extensions.Configuration.CommandLine": "1.0.0-rc2-final",

And finally, add a using statement to the file that your Main method is in:

using Microsoft.Extensions.Configuration;

Example Main method:

public static void Main(string[] args)
{
    var config = new ConfigurationBuilder()
        .AddCommandLine(args)
        .Build();

    var host = new WebHostBuilder()
        .UseKestrel()
        .UseConfiguration(config)
        .UseStartup<Startup>()
        .Build();
    host.Run();
}