【问题标题】:How to deploy ASP.NET application to production machine without having to change DB connection string manually如何将 ASP.NET 应用程序部署到生产机器而无需手动更改数据库连接字符串
【发布时间】:2019-01-02 08:06:44
【问题描述】:

目前,当我部署我的 Web 应用程序时,我总是进入我的 web.config 文件,在部署应用程序之前手动更改连接字符串中的服务器名称等。有没有更简单的方法来部署 Web 应用,而不必总是更改连接字符串中的服务器?

谢谢

【问题讨论】:

  • 你现在用什么来部署?

标签: c# asp.net


【解决方案1】:

【讨论】:

  • 当我只用一个链接发布这样的答案时,我被否决了。现在该链接不受支持且已过期。
  • 这两个链接都对我有用。哪个链接“过时”,为什么?
  • 您没有在这些页面的顶部看到消息说我们不再定期更新此内容。如何:转换 Web.config 文章的问题在于 如果不存在转换文件 ... 右键单击 Web.config 文件 不起作用。我已经发布了一个答案,希望将来对其他人有所帮助。
【解决方案2】:

这取决于您如何部署 Web 应用程序,但一种常见的方法是使用 web.config 转换

http://msdn.microsoft.com/en-us/library/dd465326%28v=vs.110%29.aspx

【讨论】:

    【解决方案3】:

    我们使用WIX installer 正是为了这个目的。

    可以自定义,就像我的例子一样,在安装时选择 DEV、QA 或 PROD 环境。

    最好的是它使用了底层的 MSI 安装框架,如果这是正确的话。

    【讨论】:

      【解决方案4】:

      假设您只需要更改一个特定 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 部署的连接字符串,那么这应该是最直接的。就是说要搜索指定名称的连接字符串,然后在部署过程中更改连接字符串的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 2021-02-16
        • 1970-01-01
        相关资源
        最近更新 更多