就目前而言,现在当我部署我的 Web 应用程序时,我总是进入我的 web.config 文件,在部署应用程序之前手动更改连接字符串中的服务器名称等。有没有更简单的方法来部署 Web 应用程序,而不必总是更改连接字符串中的服务器?
谢谢
部署 Web 应用程序项目时使用转换 Web.config:
这取决于您如何部署 Web 应用程序,但一种常见的方法是使用 web.config 转换
http://msdn.microsoft.com/en-us/library/dd465326%28v=vs.110%29.aspx
假设您只需要更改一个特定 Web 部署的连接字符串,那么您可以通过其他人所说的转换来做到这一点。以下内容应准确显示您需要执行的操作。
在解决方案资源管理器中展开属性节点以获取 PublishProperties,如下所示。
右键单击 Web Deploy 配置文件并选择Add Config Transform,如下所示。
您将在 Web Config 节点中获得一个Web.project - Web Deploy.config文件。初始内容如下。
<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on using web.config transformation visit https://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>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
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>
将示例更改为以下内容或添加以下内容。
<connectionStrings>
<add name="csname"
connectionString="yourotherconnectionstring"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
其中csname是您需要在部署中替换的连接字符串的 Web.config 中的名称。
还有许多其他可能的转换,但如果您只需要更改特定 Web 部署的连接字符串,那么这应该是最直接的。就是说要搜索具有指定名称的连接字符串,然后在部署期间更改连接字符串的值。