手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

在asp.net 2.0中的web.config文件中调用外部文件

来源:互联网 作者:west263.com 时间:2008-02-22
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

在一个工作项目或者工作小组中,有可能经常要转换工作的调试环境,比如开发环境,测试环境,部署环境,这样有可能要对web.config文件进行修改或改动,比如要改数据库的连接字符串,角色配置,安全配置环境等,一不小心,很容易会发生遗漏等错误.在asp.net 2.0的web.config文件中,新加入了可以引入外部文件的新特性,
使到我们可以先预先搞好几个文件,比如将经常要改动的部分,如数据库连接串部分等,按不同的开发环境,分别写成若干个xml文件,然后在web.config中把它们按需要调入进来.比如

我们先建立两个目录,一个叫test,一个叫developer,分别存放测试和开发时,用到的不同环境,比如
在devloper文件中建立一个developerconnectionstring.xml,内容如下

<connectionStrings>
<add name="connstr" connectionString=
"data source=.\sqlexpress;initial catalog=
northwind;integrated security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
再建立一个developerappsetingstring.xml如下<appSettings>
<add key="autoemail" value="abc@abc.com /> </appSettings>

再建立一个developermembership.xml如下

<membership defaultProvider="Northwind">
<providers>
<add name="Northwind"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="connstr"/>
</providers>
</membership>
同样道理,可以在test目录下,也建立相关类似的xml文件,然后,在web.config中,可以这样调用了
<?xml version="1.0"?>
<configuration>
<appSettings configSource="developer\developerappsetingstring.xml"/>
<connectionStrings
configSource="developer\developerconnectionstring.xml" />
<system.web>
<membership
configSource="developer\developermembership.xml"/>
<compilation debug="true"/>
<authentication mode="Forms"/>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
可以看到,在web.config中,可以通过configsource属性来读取外部文件

jackyrong BLOG

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!