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

2023-11-20 18:18
文章标签 配置 web 读写 config asp net2.0

本文主要是介绍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>");

}

上面代码只是进行简单的输出所有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 天天 阅读(834) 评论(3)   编辑  收藏 引用 网摘 所属分类: ASP.NET V2.0
<script type="text/javascript"> // </script>

FeedBack:
re: ASP.NET2.0里web.config配置的读写
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是否可用呢?

这篇关于ASP.NET2.0里web.config配置的读写的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/396507

相关文章

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

Jenkins分布式集群配置方式

《Jenkins分布式集群配置方式》:本文主要介绍Jenkins分布式集群配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装jenkins2.配置集群总结Jenkins是一个开源项目,它提供了一个容易使用的持续集成系统,并且提供了大量的plugin满

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C#读写文本文件的多种方式详解

《C#读写文本文件的多种方式详解》这篇文章主要为大家详细介绍了C#中各种常用的文件读写方式,包括文本文件,二进制文件、CSV文件、JSON文件等,有需要的小伙伴可以参考一下... 目录一、文本文件读写1. 使用 File 类的静态方法2. 使用 StreamReader 和 StreamWriter二、二进

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

如何使用Maven创建web目录结构

《如何使用Maven创建web目录结构》:本文主要介绍如何使用Maven创建web目录结构的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录创建web工程第一步第二步第三步第四步第五步第六步第七步总结创建web工程第一步js通过Maven骨架创pytho