C# 如何将ASP.NET应用程序部署到生产计算机,而不必手动更改DB连接字符串
目前,当我部署web应用程序时,我总是在部署应用程序之前进入web.config文件,手动更改连接字符串中的服务器名称等。有没有更简单的方法来部署web应用程序,而不必始终更改连接字符串中的服务器 谢谢我们正是为了这个目的而使用的 它可以定制,就像在我的例子中一样,在安装时选择DEV、QA或PROD envC# 如何将ASP.NET应用程序部署到生产计算机,而不必手动更改DB连接字符串,c#,asp.net,C#,Asp.net,目前,当我部署web应用程序时,我总是在部署应用程序之前进入web.config文件,手动更改连接字符串中的服务器名称等。有没有更简单的方法来部署web应用程序,而不必始终更改连接字符串中的服务器 谢谢我们正是为了这个目的而使用的 它可以定制,就像在我的例子中一样,在安装时选择DEV、QA或PROD env 最好的方法是它使用底层MSI安装框架,如果这个词在这里正确的话。在部署Web应用程序项目时使用transform Web.config: 这取决于您如何部署web应用程序,但一种常见的方法
最好的方法是它使用底层MSI安装框架,如果这个词在这里正确的话。在部署Web应用程序项目时使用transform Web.config:
这取决于您如何部署web应用程序,但一种常见的方法是使用web.config转换
假设您只需要更改一个特定Web部署的连接字符串,那么您可以像其他人所说的那样通过转换来完成。下面应该确切地说明您需要做什么 在解决方案资源管理器中,展开“属性”节点以获取PublishProperties,如下所示
<?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>
右键单击WebDeployProfile并选择AddConfigTransform,如下所示
<?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>
您将在Web配置节点中获得一个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>
将示例更改为以下内容或添加以下内容
<?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部署的连接字符串,那么这应该是最直接的。这意味着您要搜索具有指定名称的连接字符串,然后在部署过程中更改连接字符串值。您目前正在使用什么进行部署?当我仅用一个链接发布这样的答案时,我会被否决。现在该链接不受支持且已过期。这两个链接对我都适用。哪一个链接“过时”以及原因?你看不到这些页面顶部的消息说我们不再定期更新此内容。?How:Transform Web.config文章的问题是,如果不存在转换文件。。。右键单击Web.config文件无效。我已经发布了一个答案,我希望将来能帮助别人。