我正在编写一个可以使用几种不同主题的页面,并将每个主题的一些信息存储在web.config中。
创建一个新的sectionGroup并将所有内容存储在一起,还是仅将所有内容放入appSettings中更有效率?
使用configSection的解决方案。
<configSections>
<sectionGroup name="SchedulerPage">
<section name="Providers" type="System.Configuration.NameValueSectionHandler"/>
<section name="Themes" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<SchedulerPage>
<Themes>
<add key="PI" value="PISchedulerForm"/>
<add key="UB" value="UBSchedulerForm"/>
</Themes>
</SchedulerPage>
我使用以下代码访问configSection中的值:
NameValueCollection themes = ConfigurationManager.GetSection("SchedulerPage/Themes") as NameValueCollection;
String SchedulerTheme = themes["UB"];
appSettings解决方案
<appSettings>
<add key="PITheme" value="PISchedulerForm"/>
<add key="UBTheme" value="UBSchedulerForm"/>
</appSettings>
我使用以下代码来访问appSettings中的值:
String SchedulerTheme = ConfigurationManager.AppSettings["UBSchedulerForm"].ToString();