WebHostBuilder。 如何在.NET-Core 2.0中使用CommandLine设置URL地址?
我使用下面的代码在我的.Net-Core 1.2环境中添加CommandLine,我可以使用命令行,如dotnet run --urls "http://a.cn.com:5000" --environment "Production"
。
public static void Main(string[] args) { var config = new ConfigurationBuilder() .AddJsonFile("hosting.json", optional: true) .AddCommandLine(args) .Build(); var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup() .UseConfiguration(config) .Build(); host.Run(); }
迁移到.net核心2.0之后就变成了这样
public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) { return WebHost.CreateDefaultBuilder(args) .ConfigureLogging((context, logging) => { logging.AddSerilog(); }) .UseStartup() .Build(); }
我使用相同的命令行,它失败了。 如何在.net core 2.0中添加命令?
供应:2017.08.17 launchsetting.json如下:
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:5005/", "sslPort": 0 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "Robert.Project.Web": { "commandName": "Project", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "applicationUrl": "http://localhost:5006" } } }
我期待听的是Now listening on http://a.cn.com:5000
我得到的是:
我们不应该在Program.cs
文件中添加任何其他代码,因为CreateDefaultBuilder包含addCommandLine。 有关更多详细信息,请参阅WebHost.cs#L177
供应2017 0817 17:44 UTC + 8
[::]是IPv4 0.0.0.0的IPv6等价物。
直接使用UseConfiguration
设置由args填充的URL地址:
public static IWebHost BuildWebHost(string[] args) { var config = new ConfigurationBuilder().AddCommandLine(args).Build(); return WebHost.CreateDefaultBuilder(args) .UseConfiguration(config) .UseStartup() .Build(); }
我们不应该在Program.cs文件中添加任何其他代码,因为CreateDefaultBuilder包含addCommandLine。 有关更多详细信息,请参阅WebHost.cs#L177
这不完全正确,因为链接的CreateDefaultBuilder
实现使用ConfigureAppConfiguration
方法,而不是UseConfiguration
。 并且存在差异(来自相关的Github问题 ):
ConfigureAppConfiguration
用于在应用程序服务中配置IConfiguration
,而UseConfiguration
用于配置WebHostBuilder
的设置。 由于url地址是WebHostBuilder
上的设置,因此只有UseConfiguration
可用于此处。请注意,WebHostBuilder的配置已添加到应用程序的配置中,但反之则不然。 配置应用程序的配置不会影响WebHostBuilder的配置。
- 从Mac上的命令行运行ASP.NET 5应用程序时是使用launchSettings.json吗?
- ASP.NET 5中的Web API身份validation
- 在ASP.NET 5中将Razor视图渲染为字符串
- 在web api控制器中使用async / await或task(.net core)
- 如何在ASP.NET Core中设置强类型配置?
- ASP.Net Core 2.1中的身份:自定义AccountController
- ASP.NET Core MetaDataType属性不起作用
- 将查询参数绑定到ASP.NET Core中的模型
- ASP.Net Core MVC – 自定义属性的客户端validation