host.json的意义是什么,因为appsettings.json就足够了 [英] What's the point of hosting.json since appsettings.json is sufficient

查看:367
本文介绍了host.json的意义是什么,因为appsettings.json就足够了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET Core 2 Web API应用程序中,我可以使用 appsettings.json 覆盖配置 urls ,但是在官方文档引入了额外的文件 hosting.json,为什么呢?

In .NET Core 2 Web API app, I could override configuration urls using appsettings.json, but in the official docs they introduced extra file "hosting.json", Why? What's the point of adding complexity?

下面的代码可以使用 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();
    }
}

appsettings.json内容:

appsettings.json content:

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

侧面注释:
注释 .SetBasePath(Directory.GetCurrentDirectory( ))将使VS 2017调试模式保持运行状态(意味着应用 launchSettings.json ,并自动启动url),否则它将无法运行。我猜想它与 CreateDefaultBuilder

Side note: Commenting .SetBasePath(Directory.GetCurrentDirectory()) will keep VS 2017 debug mode operational (means apply launchSettings.json, and auto launch url) otherwise it won't. I guess its related to the CreateDefaultBuilder implementation.

推荐答案

我认为, hosting.json 是一个专门用于ASP的配置文件。网络核心应用托管。 (如果您了解有关托管的更多信息)

I think, hosting.json is a configuration file used specifically for asp.net core application hosting. (if you know more about hosting)

WebHostBuilder直接将其密钥与hosting.json文件映射,并且无法加载 config部分,就像我们在常规配置设置中一样。

WebHostBuilder directly maps its keys with the hosting.json file and it doesn't have the capability to load the config section as we do in normal configuration settings.

根据您帖子中附加的链接

According to link attached in your post


使用配置来配置主机。在以下示例中,可以在hosting.json文件中指定
主机配置。从hosting.json文件加载的任何
配置都可以被
命令行参数覆盖。

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

如果只有我们明确地指定 hosting.json ,然后才能使用dotnet命令修改WebHostBuilder的配置。

If only we explicitly us hosting.json then the WebHostBuilder configurations can be modified using dotnet command.

例如

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

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

这将覆盖hostings.json文件中的URL。

this will override the urls from hostings.json file.

希望,这可能会给您一些想法。

Hopefully, this may give some idea.


PC: hosting.json 可以重命名为 myappsettings.json ,它可以
具有配置和Web Host Builder配置。

PC: hosting.json can be renamed like myappsettings.json it can have configuration and Web Host Builder configuration.

这篇关于host.json的意义是什么,因为appsettings.json就足够了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆