C# appSettings 与 applicationSettings。appSettings 过时了?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2350893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:41:09  来源:igfitidea点击:

appSettings vs applicationSettings. appSettings outdated?

c#asp.netpropertiesappsettingsweb-deployment-project

提问by citronas

I've got some questions about two ways to save settings in the web.config.

我对在 web.config 中保存设置的两种方法有一些疑问。

Appsettings: Look in web.config

Appsettings:查看 web.config

<appSettings>
 <add key="key1" value="value1"/>
 <add key="key2" value="value2"/>
</appSettings>

Usage in code-behind:

在代码隐藏中的用法

ConfigurationManager.AppSettings["key1"];

ApplicationSettings/ Properties(autogenerated by using the 'properties'-tab in the project)
Look in web.config

ApplicationSettings/ Properties(通过使用项目中的“properties”选项卡自动生成)
在 web.config 中查找

<applicationSettings>
    <Projectname.Properties.Settings>
        <setting name="TestEnvironment" serializeAs="String">
            <value>True</value>
        </setting>
    </Projectname.Properties.Settings>
</applicationSettings>

Usage in code-behind:

在代码隐藏中的用法

Properties.Settings.Default.TestEnvironment

So, what's the difference between these two storage possibilities of settings in the web.config?
As far as I can see, a downside of the appSettings is that you have modify the web.config yourself and the appSettings are not strong typed, where as the applicationSettings are.

那么,web.config 中设置的这两种存储可能性之间有什么区别?
就我所见,appSettings 的一个缺点是你自己修改了 web.config 并且 appSettings 不是强类型的,而 applicationSettings 是。

Both are replaceable within a web deployment project.

两者都可以在 Web 部署项目中替换。

As far as I am concerned, there is no use for appSettings. Am I missing something here? Which is the historically seen older one?

就我而言,appSettings 没有用。我在这里错过了什么吗?哪个是历史上看到的更老的?

采纳答案by Nick Craver

This has been discussed before here: Pros and cons of appSettings vs applicationSettings (.NET app.config).

之前已经在这里讨论过:appSettings vs applicationSettings (.NET app.config) 的利弊

As for your questions: The older one is <appSettings>, it was around before 2.0, <applicationSettings> became available in 2.0.

至于你的问题:旧的是<appSettings>,大约在2.0之前,<applicationSettings>在2.0中可用。

Advantage? When I'm editing a value, or adding a value on a server where the best tool is notepad <applicationSettings> is veryverbose, and sometimes I just want a string. Maybe a dumb example, but when I'm tweaking the config settings between tiers to get the automatic deployment setup correctly, it's tremendously useful that it's simple.

优势?当我编辑一个值,或在最好的工具是记事本的服务器上添加一个值时,<applicationSettings>非常冗长,有时我只想要一个 string。也许是一个愚蠢的例子,但是当我在层之间调整配置设置以正确设置自动部署时,它的简单非常有用。

I have to agree with marc_sfrom the other discussion though, if you're doing anything that's really complex, you're probably approaching the point you should have your own configuration section anyway. Since you're de-serializing into your config type on startup...you get the same type checking that way, just via the XML Serializer directly is the only difference.

不过,我必须同意其他讨论中的marc_s,如果您正在做任何非常复杂的事情,那么您可能已经接近应该拥有自己的配置部分的地步。由于您在启动时反序列化为您的配置类型……您可以通过这种方式进行相同的类型检查,只是直接通过 XML Serializer 是唯一的区别。

This also has the advantage of me doing Config.LDAPServeror maybe one config for different areas each, like Security.Configand Themes.Config(guessing here!), you can get a really useful/clear naming scheme in there as a side benefit.

这也有我做的优势,Config.LDAPServer或者可能是针对不同领域的一个配置,比如Security.ConfigThemes.Config(在这里猜测!),你可以在那里获得一个非常有用/清晰的命名方案作为附带好处。

回答by Loophole

One thing I have noticed is that AppSettings values can be referenced via <%$ AppSettings: name %>inline tags in aspx pages, but there seems to be no equivalent way to access ApplicationSettingsvalues through inline tags.

我注意到的一件事是,可以通过<%$ AppSettings: name %>aspx 页面中的内联标记引用 AppSettings 值,但似乎没有ApplicationSettings通过内联标记访问值的等效方法。

回答by Bernhard Hofmann

ApplicationSettings are namespaced so two different assemblies can both have a setting for "timeout" without conflicts, and ApplicationSettings are optional since the default value is set via an attribute on the setting in code.

ApplicationSettings 是命名空间的,因此两个不同的程序集都可以有一个“超时”设置而不会发生冲突,ApplicationSettings 是可选的,因为默认值是通过代码中设置的属性设置的。

回答by galmok

I would like to add that IIS 8.0 GUI (and previous versions as well) cannot edit the <applicationSettings>section (it is invisible, i.e. it appears as if no parameters can be configured) whereas <appSettings>are editable with IIS 8.0.

我想补充一点,IIS 8.0 GUI(以及以前的版本)无法编辑该<applicationSettings>部分(它是不可见的,即看起来好像没有可以配置参数),而<appSettings>可以使用 IIS 8.0 进行编辑。

It would have been nice if VS2012/IIS 8.0 used the same GUI configure system all the way, but the products do not seem to be synchronized in that aspect. One way or the other, you may have to edit the application settings with notepad.

如果 VS2012/IIS 8.0 一直使用相同的 GUI 配置系统就好了,但产品在这方面似乎没有同步。一种或另一种方式,您可能必须使用记事本编辑应用程序设置。

The connection strings do appear in both GUIs but if using <applicationSettings>in IIS they include full path (Namespace.Properties.Settings.ConnectionStringName).

连接字符串确实出现在两个 GUI 中,但如果<applicationSettings>在 IIS 中使用,它们包含完整路径(Namespace.Properties.Settings. ConnectionStringName)。