ASP.NET2.0里web.config配置的读写

 
 
ASP.NET2.0 里不但进一步扩展了配置文件 web.config ,更为重要的是系统提供了一组 API 函数,让我们可以以编程的方式从配置文件里提取信息
    首先,先看看如果从 web.config 里提取 appSettings 里的配置值,示例代码如下:
 <appSettings>
       <add key="pagetitle" value="Job Site Starter Kit (Ver.1.0)"></add>
        <add key="sitelogo" value="logo.gif"></add>
       <add key="advertiseemail" value="sales@somesite.com"></add>
     </appSettings>
利用 ASP.NET2.0 提供的一组 API 函数,您可以很容易的获取 AppSettingsSection 里所有的 Keys/value 组对,如下:
Configuration config
= WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
AppSettingsSection appSettings = (AppSettingsSection) config.GetSection("appSettings");
string[] appKeys = appSettings.Settings.AllKeys;
 
for (int i = 0; i < appSettings.Settings.Count; i++)
{
// 这里只进行简单的输出
Response.Write(appSettings.Settings[appKeys[i]].Value);
Response.Write("<BR>");
}
 
上面代码只是进行简单的输出所有 Key value 值,然而,你可能想获取的仅仅是某一个 key 的值,这也非常简单,如下:
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
 
string pateTitle= appSettings.Settings["pagetitle"].Value; // 获取 key patetitle value
string siteLogo appSettings.Settings["siteLogo"].Value; // 获取 key sitelogo value
 
对于数据库连接字符串,在 ASP.NET2.0 里提供了专门的配置节如下:
<connectionStrings>
    <add name="connectionstring"
connectionString ="Data Source=SQLEXPRESS;AttachDbFilename=JsskDb.mdf; … .."/>
   
<add name="MyProviderConnectionString"
connectionString ="Data Source=SQLEXPRESS;Integrated Security=True;  … …"/>
</connectionStrings>
 
这样我们很容易获取数据库连接字符串如下:
Configuration config
= WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
 ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection("connectionstring ");
 
ConnectionStringSettingsCollection conCollection = conSection.ConnectionStrings;
 
foreach (ConnectionStringSettings conSetting in conCollection)
 
{
Response.Write(conSetting.ConnectionString);
Response.Write("<BR>");
}
 
另外,利用 API 函数,你同时还可以在代码里更改 web.config 数据库连接的配置的值,如下
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
 
ConnectionStringsSection conSection
 = (ConnectionStringsSection)config.GetSection("connectionStrings");
 
conSection.ConnectionStrings["SQLConnectionString"].ConnectionString =
"Data Source=SQLEXPRESS;Integrated Security=True;  … …";
config.Save();
 
 
这里最有意思的可能就是类的转换,在 <appSettings ></appSettings> 里,使用的是 AppSettingsSection 类,在 <connectionStrings></ connectionStrings> 里使用的的是 ConnectionStringsSection 类,事实上, ASP.NET2.0 提供的一组函数都是“配置节名 +Section ”的形式提供的类。
   ASP.NET 官方网站曾经对此专门介绍,可以找不到该文件了。
 
ASP.NET2.0 里提供了两种方式对数据库连接字符串加密,一种是使用 asp_regii 命令,一种是通过代码,下面显示的是通过代码方式对数据库连接字符串加密,代码如下:
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection configSection = config.GetSection("connectionStrings");
 
if (configSection.SectionInformation.IsProtected)
{// 如果已经加密,就不用再加密了
configSection.SectionInformation.UnprotectSection();
config.Save();
 
}
else
{
configSection.SectionInformation.ProtectSection ("DataProtectionConfigurationProvider");
config.Save();
}
 


这样,你检查该文件的配置可能如下:
<connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAVClqG40BZkCjK40
adynN8gQAAAACAAAAAAADZgAAqAAAABAAAABIhtOW …PE
</CipherData>
</EncryptedData>
</connectionStrings>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值