【问题标题】:web.debug.config does not workweb.debug.config 不起作用
【发布时间】:2018-02-23 03:43:09
【问题描述】:

我有一个 webb 应用程序,我想在其中利用 web.debug.config 文件,以便在调试时使用测试数据库。这对我不起作用。我的 web.debig.config 中有这个..

<connectionStrings>
  <add name="connStr" 
    connectionString="Data Source=LocalSqlserverName;Initial Catalog=testdb;Persist Security Info=True;User ID=Myusername;Password=mypassword" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

在我的 web.config 文件中,这个连接字符串指向另一个数据库服务器。但是当我调试时,我可以看到 web.config 文件中的连接字符串被使用,而不是 web.debug.config 中的连接字符串。我在这里做错了什么?

【问题讨论】:

  • 如果你部署你的应用程序,它会工作,但如果你用 VS 调试你的应用程序,它就不会工作,并且总是使用web.config 文件
  • 真的吗?从 MS 那里获得解决方案真是一个毫无价值的选择...... :(
  • @esiprogrammer 你确定吗?我本可以发誓过去成功使用它。无论如何,大多数时候我使用xdt:Transform="Replace" 而不是SetAttributes 选项,但这不重要。
  • 以前我试过了,但我没有运气,这里有一个关于这个的帖子:https://forums.asp.net/t/1532038.aspx
  • @MTplus 如果答案正确,请相应标记。您提出的其他问题也是如此。

标签: c# web-config web.config-transform


【解决方案1】:

正如@esiprogrammer 所说,它通常只在从 Visual Studio 发布时转换。

但是,您可以在构建时转换 Web.config。将此添加到您的 *.csproj 文件中:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild">
    <TransformXml 
        Source="Web.Base.config" 
        Transform="Web.$(Configuration).config" 
        Destination="Web.config" />
</Target>

将源配置保留在Web.Base.config 中。启用转换就足够了,它适用于任何 XML 配置文件。

来源:

https://stackoverflow.com/a/35561167/3850405

【讨论】:

    猜你喜欢
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 2013-11-15
    • 2012-05-08
    相关资源
    最近更新 更多