hosting.json available options

2020-06-13 06:49发布

问题:

Where can I find some documentation regarding which options are available on the hosting.json file? Right now I'm using the server.ulrs but I'm wondering if I can add the https certificate path/password on it.

My hosting.json:

{
  "server.urls": "http://0.0.0.0:80;https://0.0.0.0:443"
}

Where I'm using it:

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

回答1:

Short Answer

I'm wondering if can I add the https certificate path/password on it.

Out of the box, you cannot use hosting.json to set up your HTTPs certificate and credentials. You can, though, write custom code to support that scenario. There is a GitHub issue about that with a sample project by the Tratcher.

Where can I find ... documentation regarding ... options ... available in the hosting.json file?

The hosting.json file usually passes its options to the WebHostBuilder.UseConfiguration method.

  • ASP.NET Core hosting documentation includes a description of all recognized options.
  • GitHub.com/aspnet/hosting contains a static class with the recognized keys.

This is that static class:

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";
}

Example

For instance, the following hosting.json file...

{
    "urls": "http://localhost:12345;http://localhost:54321",
    "contentRoot": "C:\\foobar",
    "environment": "QualityAssurance"
}

...and the following entry point...

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

...leads to the following output...

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. 

Remarks

  • The hosting.json file can have any name. For instance, we can call it broccoli.json if we like.
  • Use urls instead of server.urls. The latter is listed as the DeprecatedServerUrlsKey in the GitHub.com/aspnet/hosting repository.