李万宝
欢迎光临李万宝的个人blog
posts - 147,comments - 468,trackbacks - 93

 

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>");

}

 

上面代码只是进行简单的输出所有Keyvalue值,然而,你可能想获取的仅仅是某一个key的值,这也非常简单,如下:

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");

 

string pateTitle= appSettings.Settings["pagetitle"].Value; //获取keypatetitlevalue

string siteLogo appSettings.Settings["siteLogo"].Value; //获取keysitelogovalue

 

对于数据库连接字符串,在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>

posted on 2006-05-06 11:21 天天 阅读(3087) 评论(5)  编辑 收藏 网摘 所属分类: ASP.NET V2.0

FeedBack:
2006-05-24 00:07 | DoNet鸟      
.net2.0下web.config还有一种配置方法如下:
  <configSections>    
       
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      
<section name="My_WSP.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    
</sectionGroup>
  
</configSections>
  
  
<applicationSettings>
    
<My_WSP.Properties.Settings>
      
<setting name="My_WSP_Approve_CWSApprove" serializeAs="String">
        
<value>http://localhost:1321/My_WS/WS/Approve/CWSApprove.asmx</value>
      
</setting>
    
</My_WSP.Properties.Settings>
  
</applicationSettings>
      
这样的话上面的api是否可用呢?

  回复  引用  查看    
2006-10-17 10:03 | 幻想曲.Net      
用Save方法保存时很慢
而且发现CPU占用极大,是什么原因?

  回复  引用  查看    
2006-12-21 11:49 | wapit[未注册用户]
web.config内容变动时,IIS将自动重启
  回复  引用    
2006-12-21 11:49 | wapit[未注册用户]
@幻想曲.Net
web.config内容变动时,IIS将自动重启

  回复  引用    



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 392485




相关文章:

相关链接: