我正在写一个页面,可以使用两个不同的主题,而我要存储有关在web.config中每个主题的一些信息。
是不是更有效地创建一个新的sectionGroup和储存一切融合在一起,或只是把一切的appSettings?
configSection解决方案
< configSections>< sectionGroup NAME =SchedulerPage><节名称=供应商TYPE =System.Configuration.NameValueSectionHandler/><节名称=主题式=System.Configuration.NameValueSectionHandler/>< / sectionGroup>< / configSections>< SchedulerPage><题材><添加键=PI值=PISchedulerForm/><添加键=UBVALUE =UBSchedulerForm/>< /主题>< / SchedulerPage>要在configSection访问值,我用这code:
的NameValueCollection主题= ConfigurationManager.GetSection(SchedulerPage /主题)作为NameValueCollection中; 字符串SchedulerTheme =主题[UB];的appSettings解决方案
<&的appSettings GT;<添加键=PIThemeVALUE =PISchedulerForm/><添加键=UBThemeVALUE =UBSchedulerForm/>< /的appSettings>要访问值的appSettings,我用这code
字符串SchedulerTheme = ConfigurationManager.AppSettings [UBSchedulerForm]的ToString()。解决方案
对于更复杂的配置设置,我会用一个自定义配置节,明确规定每个部分例如:
中的作用 < appMonitoring启用=真SMTPSERVER =XXX> < alertRecipients> <添加名称=我的电子邮件=me@me/> < / alertRecipient>< / appMonitoring>在您的配置类可以公开的东西你的属性,如
公共类MonitoringConfig:配置节{ [的ConfigurationProperty(SMTP,IsRequired = TRUE)] 公共字符串的smtp { {返回此[SMTP]作为字符串; } } 公共静态MonitoringConfig GetConfig() { 返回ConfigurationManager.GetSection(appMonitoring)作为MonitoringConfig }}您可以然后从code访问以如下方式配置属性。
字符串SMTP = MonitoringConfig.GetConfig()SMTP。;I'm writing a page that can use a couple of different themes, and I'm going to store some information about each theme in the web.config.
Is it more efficient to create a new sectionGroup and store everything together, or just put everything in appSettings?
configSection solution
<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>To access values in the configSection, I am using this code:
NameValueCollection themes = ConfigurationManager.GetSection("SchedulerPage/Themes") as NameValueCollection; String SchedulerTheme = themes["UB"];appSettings solution
<appSettings> <add key="PITheme" value="PISchedulerForm"/> <add key="UBTheme" value="UBSchedulerForm"/> </appSettings>To access values in appSettings, I am using this code
String SchedulerTheme = ConfigurationManager.AppSettings["UBSchedulerForm"].ToString();解决方案
For more complex configuration setup, I would use a custom configuration section that clearly defines the roles of each section for example
<appMonitoring enabled="true" smtpServer="xxx"> <alertRecipients> <add name="me" email="me@me"/> </alertRecipient> </appMonitoring>In your configuration class you can expose your properties with something like
public class MonitoringConfig : ConfigurationSection { [ConfigurationProperty("smtp", IsRequired = true)] public string Smtp { get { return this["smtp"] as string; } } public static MonitoringConfig GetConfig() { return ConfigurationManager.GetSection("appMonitoring") as MonitoringConfig } }You can then access configuration properties in the following way from your code
string smtp = MonitoringConfig.GetConfig().Smtp;
更多推荐
在web.config中存储的值
发布评论