我的应用程序的app.config文件中有一些键可以容纳电子邮件设置.我目前正在开发的应用程序有几个地方我需要发送一封电子邮件(主要是在错误检查中让我们知道发生了一些不好的事情).为了更容易获取我需要它的设置(而不是回到app.config查找要使用的文本字符串,ConfigurationManager.AppSettings["SomeKey"].ToString()我创建了一个简单的静态类来容纳一些readonly属性,返回我需要的值.这是基本结构
internal static Class myClass
{
internal static string setting1
{
get { return ConfigurationManager.AppSettings["SomeKey"].ToString(); }
}
internal static string setting2
{
get { return ConfigurationManager.AppSettings["SomeOtherKey"].ToString(); }
}
}
Run Code Online (Sandbox Code Playgroud)
我这样做是否可以接受?我找不到这个问题,但不禁想到我错过了什么.
附加信息:
我知道简单地使用Settings.Settings文件的方法.由于我工作的代码策略,我无法使用Settings.Settings文件,这将使我这样做的方式变得不必要.
编辑:通过"可接受"我的意思是它通常做或被认为是一个好主意?我意识到整个社区都不能推测我工作的可接受性.
我想从静态类中的 appsettings.json 文件中读取 url。我尝试过类似的东西
private static string Url => ConfigurationManager.GetSection("Urls/SampleUrl").ToString();
Run Code Online (Sandbox Code Playgroud)
但每当我尝试调用GetSection()方法时,我都会得到空值。
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"cs1": "some string",
"cs2": "other string"
},
"Urls": {
"SampleUrl": "google.com"
},
"AllowedHosts": "*"
Run Code Online (Sandbox Code Playgroud)
我只是想从应用程序设置中读取一些数据。根据文档,我不应该以某种方式注册我的标准 appsettings.json 文件,因为Host.CreateDefaultBuilder(args)在 Program 类中默认为我注册。