C# 如何设置Web.Config转换以适应本地和生产配置?
我面临这样一个两难境地:必须在本地计算机上为web.config设置不同的连接字符串,并进行版本转换,以使生产二进制文件在web服务器上使用machine.config 因此,我的Visual Studio解决方案中包含以下文件:C# 如何设置Web.Config转换以适应本地和生产配置?,c#,asp.net,web-config,C#,Asp.net,Web Config,我面临这样一个两难境地:必须在本地计算机上为web.config设置不同的连接字符串,并进行版本转换,以使生产二进制文件在web服务器上使用machine.config 因此,我的Visual Studio解决方案中包含以下文件: Web.Config Web.Debug.Config Web.Release.Config 在web.config中,我删除并添加了新的连接字符串 <remove name="connstring"> <add name="connstring"
Web.Config
Web.Debug.Config
Web.Release.Config
在web.config中,我删除并添加了新的连接字符串
<remove name="connstring">
<add name="connstring" ConnectionString="blahblah" />
我想做的是,在部署(通过TFS构建)到web服务器时,final web.config中没有任何内容,这样我的web应用程序就可以使用服务器上machine.config中的任何内容
如何执行此操作?您可以使用RemoveAll transform属性删除配置设置。假设您正在部署版本生成配置,您可以通过将以下内容放入web.Release.config中生成一个完全空的web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xdt:Transform="RemoveAll" />
由此产生的web.config将在顶部包含XML声明,而没有其他内容
如果只想删除某些配置子节,请将RemoveAll transform属性添加到要删除的节中。例如,以下Web.Release.Config将删除所有应用程序设置:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings xdt:Transform="RemoveAll" />
</configuration>
更多细节