Web.config的Release版本和Debug版本不一样的奥秘

 

VS编译完后,release版本的web.config(或者app.config) 为什么不一样那?

我们查看一下项目结构,会发现有两个版本的config文件存在:

打开web.Debug.config,这个文件没有什么,就是正常的配置。

打开web.Release.config文件,发现了奥秘。下面是VS自己默认创建的内容:

<?xml version="1.0" encoding="utf-8"?>

 

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

 

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

<!--

In the example below, the "SetAttributes" transform will change the value of

"connectionString" to use "ReleaseSQLServer" only when the "Match" locator

finds an attribute "name" that has a value of "MyDB".

 

<connectionStrings>

<add name="MyDB"

connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"

xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

</connectionStrings>

-->

<system.web>

<!--

In the example below, the "Replace" transform will replace the entire

<customErrors> section of your web.config file.

Note that because there is only one customErrors section under the

<system.web> node, there is no need to use the "xdt:Locator" attribute.

 

<customErrors defaultRedirect="GenericError.htm"

mode="RemoteOnly" xdt:Transform="Replace">

<error statusCode="500" redirect="InternalError.htm"/>

</customErrors>

-->

</system.web>

</configuration>

 

仔细读一下,发现这里面给出了两个例子:

第一个例子是如何找到web.Debug.config文件中connectionStrings节点下面名字为MyDB的键,然后将内容替换成指定的内容。

第二个例子是如何将web.Debug.config文件中某个节点整体替换成指定的内容。

 

另外还可以看到给出了一个网址,可以参考进行更高级的操作:

http://schemas.microsoft.com/XML-Document-Transform

 

看到这里,就明白Release版本的web.config是怎么产生的了。

原文地址:https://www.cnblogs.com/time-is-life/p/6708588.html