AppSettings 后备/默认值?
ASP.NET
对于我使用的每个 appSetting,我想指定一个值,如果在 appSettings 中找不到指定的键,则将返回该值。我本来打算创建一个类来管理它,但我想这个功能可能已经在 .NET Framework 中的某个地方了?
.NET 中是否有一个 NameValueCollection/Hash/etc 类型的类,可以让我指定一个键和一个后备/默认值——并返回键的值或指定的值?
如果有,我可以在调用它之前(从不同的地方)将 appSettings 放入该类型的对象中。
ASP.NET
For each appSetting I use, I want to specify a value that will be returned if the specified key isn't found in the appSettings. I was about to create a class to manage this, but I'm thinking this functionality is probably already in the .NET Framework somewhere?
Is there a NameValueCollection/Hash/etc-type class in .NET that will let me specify a key and a fallback/default value -- and return either the key's value, or the specified value?
If there is, I could put the appSettings into an object of that type before calling into it (from various places).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这就是我所做的。
对于布尔值和其他非字符串类型...
This is what I do.
For Boolean and other non-string types...
ASP.NET 的 ConfigurationManager 提供了该功能。您可以使用
.Get()
或.Bind( 将配置节(通过
。这还有一个好处,它可以为您进行转换(请参阅示例中的整数)。.GetSection("MySection")
检索)绑定到对象mySectionTypeInstance)示例 (NET 6)
appsettings.json
MySection.cs
Program.cs
ASP.NET's ConfigurationManager provides that functionality. You can bind your configuration section (retrieved with
.GetSection("MySection")
) to an object with either.Get<MySectionType>()
or.Bind(mySectionTypeInstance)
. This also has the benefit, that it does conversion for you (see integers in the example).Example (NET 6)
appsettings.json
MySection.cs
Program.cs
我不相信 .NET 中内置了任何东西可以提供您正在寻找的功能。
您可以创建一个基于
Dictionary
的类,该类提供TryGetValue
的重载,并带有默认值的附加参数,例如:
字符串
而不是保持通用。还有 DependencyObject 来自Silverlight 和 WPF 世界(如果有的话)。
当然,最简单的方法是使用
NameValueCollection
:key
可以是字符串索引器上的null
。但我知道在多个地方这样做很痛苦。I don't believe there's anything built into .NET which provides the functionality you're looking for.
You could create a class based on
Dictionary<TKey, TValue>
that provides an overload ofTryGetValue
with an additional argument for a default value, e.g.:You could probably get away with
string
s instead of keeping in generic.There's also DependencyObject from the Silverlight and WPF world if those are options.
Of course, the simplest way is something like this with a
NameValueCollection
:key
can benull
on the string indexer. But I understand it's a pain to do that in multiple places.您可以创建自定义配置部分并使用 DefaultValue 属性提供默认值。有关说明,请参阅此处。
You can make a custom configuration section and provide default values using the DefaultValue attribute. Instructions for that are available here.
您可以围绕 ConfigurationManager 构建逻辑,以获取检索应用程序设置值的类型化方式。这里使用 TypeDescriptor 来转换值。
You can build logic around ConfigurationManager to get a typed way to retrieve your app setting value. Using here TypeDescriptor to convert value.
我认为 C:\%WIN%\Microsoft.NET 下的 machine.config 可以做到这一点。将密钥添加到该文件作为默认值。
http://msdn.microsoft.com/en-us/library/ms228154。 ASPX
I think the machine.config under C:\%WIN%\Microsoft.NET will do this. Add keys to that file as your default values.
http://msdn.microsoft.com/en-us/library/ms228154.aspx