关于在web.config中保存设置的两种方式,我有一些问题.

Appsettings:

<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>
    <Projectname.Properties.Settings>
        <setting name="TestEnvironment" serializeAs="String">
            <value>True</value>
        </setting>
    </Projectname.Properties.Settings>
</applicationSettings>

Usage in code-behind:

Properties.Settings.Default.TestEnvironment

那么,web.config中设置的这两种存储可能性有什么不同呢?
据我所知,appSettings的一个缺点是您自己修改了web.config,并且appSettings不是强类型的,其中是应用程序设置.

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

就我而言,有no use for appSettings个.我错过什么了吗?哪个是历史上最古老的?

推荐答案

这已经在这里讨论过了:Pros and cons of 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.

Advantage? When I'm editing a value, or adding a value on a server where the best tool is notepad <applicationSettings> is very verbose, 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.

不过,我必须同意另一个讨论中的marc_s,如果你正在做任何非常复杂的事情,你可能已经接近了你应该拥有自己的配置部分的程度.因为你在启动时反序列化到你的配置类型中...通过这种方式,只需直接通过XML序列化程序就可以进行相同的类型判断,这是唯一的区别.

这还有一个好处,就是我可以为不同的区域分别配置Config.LDAPServer个或一个配置,比如Security.ConfigThemes.Config(这里猜!),你可以得到一个非常有用/清晰的命名方案,这是一个附带的好处.

Asp.net相关问答推荐

创建项目 ASP.NET Core (.NET Core) 和 ASP.NET Core (.NET Framework) 有什么区别

如何在没有 Select 按钮的情况下在 GridView 中实现全行 Select ?

在 aspx 页面中使用 if else 和 eval

asp.net:与其中的用户控件相比,控件/页面的页面生命周期顺序是什么?

多个 DataContext 类是否合适?

无法加载文件或程序集 'System.Web.Mvc,版本 = 3.0.0.0,Elmah.MVC 问题

如何在 Visual Studio 2017 中使用 NPM 并安装包?

如何从 URI 中删除 PROTOCOL

对于每个请求,RestClient 应该是单例还是新的

从后面的 asp.net 代码中读取表单身份验证 cookie

如何设置默认页面 asp.net

无法序列化会话状态

我的 asp.net mvc Web 应用程序中的 OutputCache 设置.防止缓存的多种语法

单击按钮时播放哔声

将当前谷歌 map 保存为图片

为什么混合 Razor Pages 和 VueJs 是一件坏事?

VS 2010 中缺少 App_Code 文件夹

模型是否包含字段而不将其添加到数据库中?

如何加密 web.config 中的一项

我可以在加载事件时从 C# 的 div 标记中动态添加 HTML 吗?