在哪里可以找到有关hosting.json
文件上哪些选项可用的一些文档?现在,我正在使用server.ulrs
,但我想知道是否可以在其上添加https证书路径/密码。
我的hosting.json
:
{
"server.urls": "http://0.0.0.0:80;https://0.0.0.0:443"
}
我在哪里使用它:
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", optional: true) // <<<<<<<<< LOADING FILE
.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseConfiguration(config) // <<<<<<<<<<< USING IT
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
最佳答案
简短答案
开箱即用,您不能使用hosting.json来设置HTTPs证书和凭据。但是,您可以编写自定义代码来支持该方案。 Tratcher有一个带有示例项目的GitHub issue about that。
通常,hosting.json文件会将其选项传递给WebHostBuilder.UseConfiguration
方法。
这是该静态类:
public static class WebHostDefaults
{
public static readonly string ApplicationKey = "applicationName";
public static readonly string StartupAssemblyKey = "startupAssembly";
public static readonly string DetailedErrorsKey = "detailedErrors";
public static readonly string EnvironmentKey = "environment";
public static readonly string WebRootKey = "webroot";
public static readonly string CaptureStartupErrorsKey = "captureStartupErrors";
public static readonly string ServerUrlsKey = "urls";
public static readonly string ContentRootKey = "contentRoot";
}
例子
例如,下面的hosting.json文件...
{
"urls": "http://localhost:12345;http://localhost:54321",
"contentRoot": "C:\\foobar",
"environment": "QualityAssurance"
}
...以及以下入口点...
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile("hosting.json", optional: false)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
...导致以下输出...
PS C:\temp> dotnet run
Hosting environment: QualityAssurance
Content root path: C:\foobar
Now listening on: http://localhost:12345
Now listening on: http://localhost:54321
Application started. Press Ctrl+C to shut down.
评论
urls
代替server.urls
。后者在GitHub.com/aspnet/hosting存储库中列为DeprecatedServerUrlsKey
。