在web.config中存储的值

编程入门 行业动态 更新时间:2024-10-26 12:34:42
本文介绍了在web.config中存储的值 - 的appSettings或configSection - 这是更有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在写一个页面,可以使用两个不同的主题,而我要存储有关在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中存储的值

本文发布于:2023-11-09 06:17:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571606.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:web   config

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!