Web.Release.config 替换

本文介绍了如何使用XDT进行Web.config变换,包括替换所有AppSettings、替换特定AppSetting的值及设置CompilationDebug为true等常见操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

  Web.config变换是启动设定,所以你的每个编译链接设置都有一个设置“delta”(默认的是Debug和Release)。当你建立你的解决方案(比如当你发布一个包的时候)你原始的Web.config会根据你的Web.debug.config文件中的设置变换(以debug设置为例)。

  有很多日志和MSDN可供你参考,但是我想我应该写一个简明扼要的帖子,它可以让你熟悉怎样进行一些常见的web.config变换。

  首先,回顾一下XDT的简单知识(那个人很伟大)

  因为它属于web.config替换,所以你得知道每个XML元素可以有两个xdt属性:xdt:Tranform 和 xdt:Locator。

  变换属性:你想对XML元素干什么?
  你可能想替换它,可能想设置一个属性(SetAttribute),或者移除一个属性(RemoveAttribute),等等。

  属性定位:需要变换的元素在哪里?

  你可能想要转换一个符合特殊属性值的元素。

  例1:替换所有的AppSettings

  这是一个极端的情况,只能在你想替换web.config的整个部分的时候用。这种情况我将替换web.config中所有的AppSettings并在web.release.config中进行新的设定(这句英文可能有问题,语法不通顺!)。下面是我的基线web.config appSettings:

    
1 . < appSettings >
2 . < add key = " KeyA " value = " ValA " />
3 . < add key = " KeyB " value = " ValB " />
4 . </ appSettings >

  现在在我的web.relaese.config文件里面,我应该创建一个appSettings程序,但是我把属性xdt:Transform设置成了”Replace”因为我想替换整个元素。我没有必要用xdt:Locator属性,因为没有什么东西需要去定位—我只是想清除原来的内容并替换所有的东西。

    
< appSettings xdt:Transform = " Replace " >
< add key = " ProdKeyA " value = " ProdValA " />
< add key = " ProdKeyB " value = " ProdValB " />
< add key = " ProdKeyC " value = " ProdValC " />
</ appSettings >

  请注意在这个web.release.config文件里我的appSettings程序段有三个键而不是两个,而且他们也不尽相同。现在让我们看看当我们发布的时候在产生的web.config文件中发生了什么:

    
< appSettings >
< add key = " ProdKeyA " value = " ProdValA " />
< add key = " ProdKeyB " value = " ProdValB " />
< add key = " ProdKeyC " value = " ProdValC " />
</ appSettings >

  正如我们所预料的-- web.config appSettings被web.release config中的值替换了。这很简单!

  例2:替换一个特定的AppSetting的值

  例1可以说是一杆子打倒一片的做法,那么怎么做一点更加实际的东西呢?让我们回到原始的AppSettings web.config例子:

    
< appSettings >
< add key = " KeyA " value = " ValA " />
< add key = " KeyB " value = " ValB " />
</ appSettings >

  这次我们想要用更加适合产品环境的值来代替KeyB的键值。我们需要用到xdt:Transform 和 xdt:Locator两个属性。

  我们的办法是定义一个appSettings程序段。开头如下:

    
< appSettings >
< add key = " KeyB " value = " ProdValA " />
</ appSettings >

  现在我们要添加变换,我们想替换任何跟这个键(KeyB)符合的appSetting。

    
< appSettings >
< add key = " KeyB " value = " ProdValA " xdt:Transform = " Replace "
xdt:Locator
= " Match(key) " />
</ appSettings >

  一旦我们发布,最后生成的Web.config文件看起来如下:

    
< appSettings >
< add key = " KeyA " value = " ValA " />
< add key = " KeyB " value = " ProdValA " />
</ appSettings >

  非常棒—我们替换了keyB键却保留了keyA键(和其他任何键,如果他们存在的话)。

  例3:Compilation Debug设置成”true”

  这个情况简单,因为微软公司把即开即用的软件基础架构给了我们—但是我还是想把它在这里再写一遍,因为它阐明了一个常见的情况,而且表明如果有更多的变换,那么就把Compilation Debug设置成”true”。

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

  也有很多方法去进行设定SetAttributes,移除元素,插入元素,等等。

  学无止境

  显然,这只是些浅显的介绍,但是这就是现在我所需要涉及的深度。在我下次写帖子之前,你可以去看看web.config 变换的msdn参考文献,地址是:

  http://msdn.microsoft.com/en-us/library/dd465326%28VS.100%29.aspx

failed to req API:/nacos/v1/ns/instance after all servers([127.0.0.1:8848]) tried: failed to req API:127.0.0.1:8848/nacos/v1/ns/instance. code:403 msg: <html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Wed Jun 25 15:26:48 CST 2025</div><div>There was an unexpected error (type=Forbidden, status=403).</div><div>unknown user!</div></body></html> at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:467) ~[nacos-client-1.1.4.jar:na] at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:389) ~[nacos-client-1.1.4.jar:na] at com.alibaba.nacos.client.naming.net.NamingProxy.registerService(NamingProxy.java:191) ~[nacos-client-1.1.4.jar:na] at com.alibaba.nacos.client.naming.NacosNamingService.registerInstance(NacosNamingService.java:207) ~[nacos-client-1.1.4.jar:na] at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register(NacosServiceRegistry.java:64) ~[spring-cloud-alibaba-nacos-discovery-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.register(AbstractAutoServiceRegistration.java:239) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration.register(NacosAutoServiceRegistration.java:76) ~[spring-cloud-alibaba-nacos-discovery-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.start(AbstractAutoServiceRegistration.java:138) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.bind(AbstractAutoServiceRegistration.java:101) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:88) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:47) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:165) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at com.atguigu.yygh.hosp.ServiceHospApplication.main(ServiceHospApplication.java:16) [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.1.RELEASE.jar:2.2.1.RELEASE] Process finished with exit code 1
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值