国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

web.config文件自定義配置節的使用方法的一個簡單例子

2024-07-21 02:07:08
字體:
來源:轉載
供稿:網友
web.config文件自定義配置節的使用方法的一個簡單例子
用來演示的程序名為myapp,namespace也是myapp

1。編輯web.config文件

添加以下內容,聲明一個section

<configsections>
   <section name="appconfig" type="myapp.appconfig, myapp" />
</configsections>  

聲明了一個叫appconfig的section

2。編輯web.config文件

添加以下內容,加入一個section

<appconfig>
  <add key="connectionstring" value="this is a connectionstring" />
  <add key="usercount" value="199" />
</appconfig> 

這個section包括兩個 key

3。從iconfigurationsectionhandler派生一個類,appconfig

實現create方法,代碼如下

public class appconfig : iconfigurationsectionhandler
{
  static string m_connectionstring = string.empty;
  static int32 m_usercount = 0;
  public static string connectionstring
  {
   get
   {
    return m_connectionstring;
   }
  }
  public static int32 usercount
  {
   get
   {
    return m_usercount;
   }
  }

  static string readsetting(namevaluecollection nvc, string key, string defaultvalue)
  {
   string thevalue = nvc[key];
   if(thevalue == string.empty)
    return defaultvalue;

   return thevalue;
  }

  public object create(object parent, object configcontext, xmlnode section)
  {
   namevaluecollection settings;
  
   try
   {
    namevaluesectionhandler basehandler = new namevaluesectionhandler();
    settings = (namevaluecollection)basehandler.create(parent, configcontext, section);
   }
   catch
   {
    settings = null;
   }
  
   if ( settings != null )
   {
    m_connectionstring = appconfig.readsetting(settings, "connectionstring", string.empty);
    m_usercount = convert.toint32(appconfig.readsetting(settings, "usercount", "0"));
   }
  
   return settings;
  }
}

我們把所有的配置都映射成相應的靜態成員變量,并且是寫成只讀屬性,這樣程序通過

類似appconfig.connectionstring就可以訪問,配置文件中的項目了

4。最后還要做一件事情

在global.asax.cs中的application_start中添加以下代碼

system.configuration.configurationsettings.getconfig("appconfig");

這樣在程序啟動后,會讀取appconfig這個section中的值,系統會調用你自己實現的iconfigurationsectionhandler接口來讀取配置
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 商都县| 芷江| 沈丘县| 镇安县| 永春县| 北辰区| 邯郸市| 双柏县| 武陟县| 延庆县| 固镇县| 安仁县| 唐山市| 黑龙江省| 轮台县| 瑞丽市| 平远县| 钦州市| 东平县| 芜湖县| 宜宾市| 双鸭山市| 泊头市| 古蔺县| 方正县| 南陵县| 桂阳县| 和硕县| 江油市| 成安县| 城市| 大冶市| 通山县| 宁安市| 乌审旗| 吐鲁番市| 林西县| 广河县| 太仓市| 南丰县| 个旧市|