Web.Debug.config不会将连接字符串转换为MVC 5项目中的Web.config

5 .net asp.net asp.net-mvc web-config visual-studio

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

阅读几篇文章,我真的没有看到我真正需要做的是为了让它从Web.Debug.config获取我的值并替换当前的Web.config.

我一直在寻找另一个工作项目,它做到了这一点并且工作正常,但我经历了很多属性和设置,我没有看到有什么不同.

我可以右键单击Web.Debug.config和Preview,它会告诉我它将用"10.10.10.10"替换"test",所以它对我来说似乎很好(就像它应该工作但运行项目它不会改变值)

项目:

调试/任何CPU,运行谷歌浏览器,问题是数据源没有被更改

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Nik*_*lay 12

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

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

  • &lt;Target Name="BeforeBuild"&gt; &lt;TransformXml Source="Web.template.config" Transform="Web.$(Configuration).config" Destination="Web.config" /&gt; &lt;/Target&gt; (3认同)

mik*_*ike 6

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

将所有调试设置放在Web.config 中,将发布设置放在Web.Release.config 中,不要使用Web.Debug.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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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