Which host.json point with appsettings.json is sufficient

In the .NET API application for the .NET Core 2 web applications, I can redefine the urls configuration using appsettings.json , but in the appsettings.json papers they presented an additional file called β€œhosting.json”, why? What is the point of adding complexity?

Below code fully works with appsettings.json :

 public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) //see Side note below .AddJsonFile("appsettings.json", optional: true) .AddCommandLine(args) .Build(); return WebHost.CreateDefaultBuilder(args) .UseConfiguration(config) .UseStartup<Startup>() .Build(); } } 

The content of appsettings.json:

 { "Logging": { "IncludeScopes": false, "Debug": { "LogLevel": { "Default": "Warning" } }, "Console": { "LogLevel": { "Default": "Warning" } } }, "urls": "http://*:5005/" } 

Side note: Commenting .SetBasePath(Directory.GetCurrentDirectory()) will support VS 2017 debugging mode (means apply launchSettings.json and automatically launch url), otherwise it will not happen. I believe this is due to the implementation of CreateDefaultBuilder .

+3
source share
1 answer

I think hosting.json is a configuration file used specifically for hosting the main asp.net applications. (if you know more about hosting)

WebHostBuilder directly displays its keys with the host.json file and does not have the ability to load the config section, as in normal configuration settings.

According to the link attached to your message

Use the configuration to configure the host. In the following example, the host configuration is optionally specified in the host.json file. Any configuration loaded from the hosting.json file can be overridden by command line arguments.

If only we explicitly use hosting.json , then the configurations of WebHostBuilder can be changed using the dotnet command.

eg

dotnet run --urls "http: // *: 8080"

this will override the url from the hostings.json file.

Hope this can give some idea.

PC: hosting.json can be renamed as myappsettings.json , it can have a web host configuration and configuration.

-1
source

Source: https://habr.com/ru/post/1268113/


All Articles