web.config主要是我们为站点进行参数配置的文件,我们一般修改比较多的地方是我们数据库连接字符串connectionString、配置串appSettings等,这里主要介绍下Debug和Release两种模式下我们如何配置。
web.config源文件
<connectionStrings>
<add name="MyDbContext" connectionString="Data Source=localhost;port=1080;Initial Catalog=erp;user id=root;password=root;Charset=utf8" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
测试环境web.debug.config
<connectionStrings>
<add name="MyDbContext"
connectionString="Data Source=测试库;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
生产环境web.release.config
<connectionStrings>
<add name="MyDbContext"
connectionString="Data Source=正式库;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
特别要注意的地方: xdt:Transform="SetAttributes" xdt:Locator="Match(name)"
通过上面的配置,我们就可以进行发布了,合并后的web.config(release模式下)
发布release版本后的结果
<connectionStrings>
<add name="MyDbContext" connectionString="Data Source=正式库;Initial Catalog=MyReleaseDB;Integrated Security=True" providerName="MySql.Data.MySqlClient" />
</connectionStrings>