ASP.NET 核心 2.0 独立版:通过命令行传递监听 url
ASP.NET core 2.0 standalone: passing listening url via command line
我正在按照 this tutorial. However my specific question is about the code you get in the Program.cs file when you create a standard ASP.NET Core Web Application in VS2017, it is the same code described here:
编写我的第一个 ASP.NET Core 2.0 web REST API 应用程序
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace WebApplication5
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
当我在 VS2017 中调试时,我的应用程序运行良好,所以下一步我做的是根据 this tutorial 使其成为一个独立的应用程序,它运行良好并为我提供了一个我可以执行的可执行文件 运行(我在 windows 10 x64)。
现在的问题是此可执行文件在端口 5000 上启动网络服务器,但我希望能够通过命令行参数配置侦听 url。
通过查看上面的代码,我们可以看到 args
被传递给了 WebHost.CreateDefaultBuilder(args)
,所以我假设任何命令行参数都由这个函数解释,但是我无法弄清楚为了让服务器监听另一个端口,我必须在命令行上传递什么。
我尝试了以下选项:
- MyApp.exe --UseUrls="http://*:5001"
- MyApp.exe --UseUrls=http://*:5001
- MyApp.exe --server.urls=http://*:5001
- MyApp.exe urls="http://*:5001"
以及其他各种类似的组合...应用程序启动但仅在端口 5000 上侦听。
我开始认为我正在尝试一些不可能的事情 :) 那么这是真的不可能还是我遗漏了什么?
在 linux 中,我使用的是:./MYAPP urls=http://*:8081 &
,但您需要为此修改代码。尝试相应地更改您的代码:
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
var configuration = new ConfigurationBuilder().AddCommandLine(args).Build();
return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(configuration)
.UseStartup<Startup>()
.Build();
}
我正在按照 this tutorial. However my specific question is about the code you get in the Program.cs file when you create a standard ASP.NET Core Web Application in VS2017, it is the same code described here:
编写我的第一个 ASP.NET Core 2.0 web REST API 应用程序using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace WebApplication5
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
当我在 VS2017 中调试时,我的应用程序运行良好,所以下一步我做的是根据 this tutorial 使其成为一个独立的应用程序,它运行良好并为我提供了一个我可以执行的可执行文件 运行(我在 windows 10 x64)。
现在的问题是此可执行文件在端口 5000 上启动网络服务器,但我希望能够通过命令行参数配置侦听 url。
通过查看上面的代码,我们可以看到 args
被传递给了 WebHost.CreateDefaultBuilder(args)
,所以我假设任何命令行参数都由这个函数解释,但是我无法弄清楚为了让服务器监听另一个端口,我必须在命令行上传递什么。
我尝试了以下选项:
- MyApp.exe --UseUrls="http://*:5001"
- MyApp.exe --UseUrls=http://*:5001
- MyApp.exe --server.urls=http://*:5001
- MyApp.exe urls="http://*:5001"
以及其他各种类似的组合...应用程序启动但仅在端口 5000 上侦听。
我开始认为我正在尝试一些不可能的事情 :) 那么这是真的不可能还是我遗漏了什么?
在 linux 中,我使用的是:./MYAPP urls=http://*:8081 &
,但您需要为此修改代码。尝试相应地更改您的代码:
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
var configuration = new ConfigurationBuilder().AddCommandLine(args).Build();
return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(configuration)
.UseStartup<Startup>()
.Build();
}