How to start a ASP.NET Core 1.0 RC2 app that doesn't listen to the localhost

夙愿已清 提交于 2020-01-11 09:51:32

问题


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();
}


来源:https://stackoverflow.com/questions/37289816/how-to-start-a-asp-net-core-1-0-rc2-app-that-doesnt-listen-to-the-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!