为Debug和Release分别设置Web.config

需求:在开发asp.net应用程序时,往往想在debug和release环境下使用不同的配置,而web.config文件却只有一个

解决方案:可以在原来的web.config中写下debug环境下的配置,然后在web.release.config中写下release环境下特有的配置。常见情况写法举例如下:

1.替换某节点的某属性值,使用“SetAttributes”转换将更改 “connectionString”的值,仅在“Match”定位器查找到属性为“name”的值为“MyDB”时使用此节点的“connectionString”的值。其他不用改变的属性值无需填写。

    <connectionStrings>
      <add name="MyDB" 
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    connectionStrings>

 

2.替换整个节点,“Replace”转换将替换web.config 文件的整个 节。 请注意,由于在 节点下仅有一个 customErrors 节,因此不需要使用“xdt:Locator”特性。

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      customErrors>

3.删除某个节点的某个属性,使用RemoveAttributes。删除compilation节点的debug这个属性。

<compilation xdt:Transform="RemoveAttributes(debug)" />


以上经过测试可用。

总结:通过在web.release.config或web.debug.config中按指定的格式查找和替换节点内容,可以实现web.config按生成环境的不同生成不同的版本,方便调试和发布。

 

 

转载于:https://www.cnblogs.com/efanscai/p/3347297.html

你可能感兴趣的:(为Debug和Release分别设置Web.config)