【问题标题】:Web.Debug.config is NOT transforming the connectionstrings to the Web.config in MVC 5 projectsWeb.Debug.config 不会将连接字符串转换为 MVC 5 项目中的 Web.config
【发布时间】:2016-08-19 11:07:55
【问题描述】:

我创建了一个新的 VS 2015 Web 项目 MVC5。默认情况下,我可以看到 Web.configWeb.Debug.config

阅读了几篇文章,我真的不知道我真正需要做什么才能让它从 Web.Debug.config 中获取我的值并替换当前的 Web.config。

我一直在查看另一个工作中的项目,它可以做到这一点并且效果很好,但是我已经通过了很多属性和设置,我看不出有什么不同。

我可以右键单击 Web.Debug.config 和预览,它显示它将用“10.10.10.10”替换“测试”,所以这对我来说似乎很好(就像它应该工作但运行项目它不会改变值)

例子

项目:

调试/任何 CPU,运行 google chrome,问题是 数据源 没有改变

Web.Debug.config

 <connectionStrings>
    <add name="Envy" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
            xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    <add name="EnvyIdentity" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    <add name="DNNSmartstore" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    <add name="DNNPos" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=DevFood_POS;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>

Web.config

  <connectionStrings>
    <add name="Envy" connectionString="Data Source=test\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
    <add name="EnvyIdentity" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
    <add name="DNNSmartstore" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
    <add name="DNNPos" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=DevFood_POS;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
  </connectionStrings>

【问题讨论】:

  • 您是在 Release 中构建它吗?如果是这样,它使用 Web.Release.config 而不是 Debug
  • 我没有使用 Release。在本地另一个我没有创建的项目,我可以看到当我更改 Web.debug.config 时,然后在 Debug 中单击“播放”(运行)并且 web.config 确实得到了更新,而我的应用程序没有,我在配置或属性中的任何地方都看不到任何代码,添加了任何类型的 msbuild 东西 - 哦,等等,我应该查看 csproj 文件,等等

标签: asp.net .net asp.net-mvc visual-studio web-config


【解决方案1】:

开箱即用,转换(调试/发布)应用于发布(部署)。不在构建时,在部署时。

要在构建时实现这一点,您可能需要对项目文件进行一些手动编辑。看看这里的例子:https://gist.github.com/EdCharbeneau/9135216

【讨论】:

  • 好的,看看我对其他人的评论,也许是对 csproj 文件的编辑,我希望这可能会添加 msbuild 的东西
  • 目标>
  • 以上是其他项目使它工作的原因,所以在这和你的文章之间我可以弄清楚,很酷,谢谢
  • Ed Charbeneau 的流程有效,但我现在遇到的问题是转换在发布(部署)时被双重应用。有谁知道如何解决这个问题?
【解决方案2】:

如此接近但向后。对于这种情况,请仅使用 Web.configWeb.Release.config

将所有 Debug 设置放入 Web.config,将 Release 设置放入 Web.Release.config,不要使用 Web.Debug.config 。将使用常规 Web.config 进行调试,并且只有已发布的版本才能获得发布设置。

从你上面的例子:

Web.config

<!-- test/debug settings in regular web.config -->
    <connectionStrings>
        <add name="Envy" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
        <add name="EnvyIdentity" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
        <add name="DNNSmartstore" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
        <add name="DNNPos" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=DevFood_POS;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>       
      </connectionStrings>

Web.Release.config

<connectionStrings>
    <add name="Envy" connectionString="Data Source=test\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    <add name="EnvyIdentity" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    <add name="DNNSmartstore" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    <add name="DNNPos" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=DevFood_POS;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>

Web.Debug.config 是放置部署到测试服务器时要应用的设置的地方。这个名字起初很容易误导。我使用此功能的唯一地方是如果我有一个需要与我的开发框不同的设置的测试服务器。 :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2019-03-21
    • 2015-11-28
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多