Kestrel 配置使用特定端口 + url
Kestrel configuration to use a specific port + url
我在带有 VS2017 (15.3.5) 的 Win 7 上使用 Asp.Net core 2.0.2。
我当前的 Kestrel 配置如下所示:
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureAppConfiguration((hostContext, config) =>
{
var envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
config.Sources.Clear();
config.AddJsonFile("appsettings.json", optional : false);
config.AddJsonFile($"appsettings.{envName}.json", optional : false);
config.AddEnvironmentVariables();
})
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, 5859);
})
.UseContentRoot(pathToContentRoot)
.Build();
这显然是在 http://localhost:5859
上监听的。我想配置 Kestrel,使其仅侦听自定义 URL,例如 http://localhost:5859/MyNewApp
。我该怎么做?
(在 Core 1.0 中,我使用 UseUrls("http://localhost:5859/MyNewApp")
部分完成了工作。它会监听 http://localhost:5859
以及 http://localhost:5859/MyNewApp
。在 Core 2.0.2 中做同样的事情结果异常:
System.InvalidOperationException: A path base can only be configured using IApplicationBuilder.UsePathBase())
对于 2.0,您需要利用 UsePathBase
,因为 UseUrls
是 removed from Kestrel。您需要在启动时在 Configure
方法中执行此操作:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UsePathBase("/MyNewApp");
}
我在带有 VS2017 (15.3.5) 的 Win 7 上使用 Asp.Net core 2.0.2。
我当前的 Kestrel 配置如下所示:
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureAppConfiguration((hostContext, config) =>
{
var envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
config.Sources.Clear();
config.AddJsonFile("appsettings.json", optional : false);
config.AddJsonFile($"appsettings.{envName}.json", optional : false);
config.AddEnvironmentVariables();
})
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, 5859);
})
.UseContentRoot(pathToContentRoot)
.Build();
这显然是在 http://localhost:5859
上监听的。我想配置 Kestrel,使其仅侦听自定义 URL,例如 http://localhost:5859/MyNewApp
。我该怎么做?
(在 Core 1.0 中,我使用 UseUrls("http://localhost:5859/MyNewApp")
部分完成了工作。它会监听 http://localhost:5859
以及 http://localhost:5859/MyNewApp
。在 Core 2.0.2 中做同样的事情结果异常:
System.InvalidOperationException: A path base can only be configured using IApplicationBuilder.UsePathBase())
对于 2.0,您需要利用 UsePathBase
,因为 UseUrls
是 removed from Kestrel。您需要在启动时在 Configure
方法中执行此操作:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UsePathBase("/MyNewApp");
}