【问题标题】:How to start a ASP.NET Core 1.0 RC2 app that doesn't listen to the localhost如何启动不监听本地主机的 ASP.NET Core 1.0 RC2 应用程序
【发布时间】:2016-09-14 08:41:09
【问题描述】:

我怎样才能启动ASP.NET Core with the dotnet CLI samples 以便他们不听本地主机?

此命令不起作用:

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

【问题讨论】:

  • “不起作用”是什么意思?

标签: asp.net .net asp.net-core .net-core .net-core-rc2


【解决方案1】:

您要执行的操作需要您在应用程序的 Main 方法中将命令行参数添加到您的配置中。在创建 WebHostBuilder 对象之前添加类似这样的内容:

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

然后在调用 .Build() 之前将其添加到 WebHostBuilder 对象:

.UseConfiguration(config)

您还需要向 project.json 添加一个依赖项:

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

最后,将 using 语句添加到您的 Main 方法所在的文件中:

using Microsoft.Extensions.Configuration;

例如Main方法:

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

【讨论】:

  • 也可以直接在代码中配置url,比如this samle:.UseKestrel().UseUrls("http://*:5000")
猜你喜欢
  • 1970-01-01
  • 2022-01-27
  • 2019-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-27
  • 1970-01-01
  • 2019-07-04
相关资源
最近更新 更多