Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C将整个appSettings文件绑定到类_C#_Configuration_Binding_Appsettings - Fatal编程技术网

C# C将整个appSettings文件绑定到类

C# C将整个appSettings文件绑定到类,c#,configuration,binding,appsettings,C#,Configuration,Binding,Appsettings,在C中,我们可以将appSettings中的一些设置绑定到类,例如: var connectionStrings = new ConnectionStrings(); var sectionConnectionString = Configuration.GetSection("ConnectionStrings"); 在appsettings中,它如下所示: { "Logging": { "LogLevel": {

在C中,我们可以将appSettings中的一些设置绑定到类,例如:

var connectionStrings = new ConnectionStrings();
var sectionConnectionString = Configuration.GetSection("ConnectionStrings");
在appsettings中,它如下所示:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
当我想绑定日志时,我需要调用另一个绑定:

Configuration.GetSection("Logging");
如何绑定整个appsettings文件?带有空字符串的GetSection不起作用:

Configuration.GetSection("");

您的配置需要一个类,之后您可以使用它,您不需要映射每个设置,只需映射您需要的设置:

var configObject=Configuration.Get; 配置对象示例:

公共类配置对象{ 公共日志记录{get;set;} 公共字符串AllowedHosts{get;set;} 公共连接字符串连接字符串{get;set;} } 公共类日志记录{ 公共日志级别日志级别{get;set;} } 公共类日志级别{ 公共字符串默认值{get;set;} } 公共类连接字符串{ 公共字符串ConnString1{get;set;} } 提示:
如果您不使用aspnetcore,您可能还需要包含此NuGet软件包:

您的配置需要一个类,之后您可以使用它,您不需要映射每个设置,只需映射您需要的设置:

var configObject=Configuration.Get; 配置对象示例:

公共类配置对象{ 公共日志记录{get;set;} 公共字符串AllowedHosts{get;set;} 公共连接字符串连接字符串{get;set;} } 公共类日志记录{ 公共日志级别日志级别{get;set;} } 公共类日志级别{ 公共字符串默认值{get;set;} } 公共类连接字符串{ 公共字符串ConnString1{get;set;} } 提示:
如果您不使用aspnetcore,您可能还需要包含此NuGet包:

您可以按原样使用配置实例

可以将设置绑定到类:

var appSettings = Configuration.Get<AppSettings>();
也可以使用选项模式插入设置

services.Configure<AppSettings>(Configuration);

您可以按原样使用配置实例

可以将设置绑定到类:

var appSettings = Configuration.Get<AppSettings>();
也可以使用选项模式插入设置

services.Configure<AppSettings>(Configuration);