在ASP.NET2.0中的WEB.CONFIG文件中调用外部文件

80酷酷网    80kuku.com

  asp.net|web

  在一个工作项目或者工作小组中,有可能经常要转换工作的调试环境,比如开发环境,测试环境,部署环境,这样有可能要对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=" /> </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

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: