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

相关文章

Zookeeper安装和配置说明

一、Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式。 ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境; ■ 伪集群模式:就是在一台物理机上运行多个Zookeeper 实例; ■ 集群模式:Zookeeper运行于一个集群上,适合生产环境,这个计算机集群被称为一个“集合体”(ensemble) Zookeeper通过复制来实现

CentOS7安装配置mysql5.7 tar免安装版

一、CentOS7.4系统自带mariadb # 查看系统自带的Mariadb[root@localhost~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x86_64# 卸载系统自带的Mariadb[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7

hadoop开启回收站配置

开启回收站功能,可以将删除的文件在不超时的情况下,恢复原数据,起到防止误删除、备份等作用。 开启回收站功能参数说明 (1)默认值fs.trash.interval = 0,0表示禁用回收站;其他值表示设置文件的存活时间。 (2)默认值fs.trash.checkpoint.interval = 0,检查回收站的间隔时间。如果该值为0,则该值设置和fs.trash.interval的参数值相等。

NameNode内存生产配置

Hadoop2.x 系列,配置 NameNode 内存 NameNode 内存默认 2000m ,如果服务器内存 4G , NameNode 内存可以配置 3g 。在 hadoop-env.sh 文件中配置如下。 HADOOP_NAMENODE_OPTS=-Xmx3072m Hadoop3.x 系列,配置 Nam

10. 文件的读写

10.1 文本文件 操作文件三大类: ofstream:写操作ifstream:读操作fstream:读写操作 打开方式解释ios::in为了读文件而打开文件ios::out为了写文件而打开文件,如果当前文件存在则清空当前文件在写入ios::app追加方式写文件ios::trunc如果文件存在先删除,在创建ios::ate打开文件之后令读写位置移至文件尾端ios::binary二进制方式

wolfSSL参数设置或配置项解释

1. wolfCrypt Only 解释:wolfCrypt是一个开源的、轻量级的、可移植的加密库,支持多种加密算法和协议。选择“wolfCrypt Only”意味着系统或应用将仅使用wolfCrypt库进行加密操作,而不依赖其他加密库。 2. DTLS Support 解释:DTLS(Datagram Transport Layer Security)是一种基于UDP的安全协议,提供类似于

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

Java Web指的是什么

Java Web指的是使用Java技术进行Web开发的一种方式。Java在Web开发领域有着广泛的应用,主要通过Java EE(Enterprise Edition)平台来实现。  主要特点和技术包括: 1. Servlets和JSP:     Servlets 是Java编写的服务器端程序,用于处理客户端请求和生成动态网页内容。     JSP(JavaServer Pages)

BUUCTF靶场[web][极客大挑战 2019]Http、[HCTF 2018]admin

目录   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 [web][HCTF 2018]admin 考点:弱密码字典爆破 四种方法:   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 访问环境 老规矩,我们先查看源代码

【STM32】SPI通信-软件与硬件读写SPI

SPI通信-软件与硬件读写SPI 软件SPI一、SPI通信协议1、SPI通信2、硬件电路3、移位示意图4、SPI时序基本单元(1)开始通信和结束通信(2)模式0---用的最多(3)模式1(4)模式2(5)模式3 5、SPI时序(1)写使能(2)指定地址写(3)指定地址读 二、W25Q64模块介绍1、W25Q64简介2、硬件电路3、W25Q64框图4、Flash操作注意事项软件SPI读写W2