web.debug.config 不起作用

MTp*_*lus 2 c# web-config web.config-transform

我有一个 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>
Run Code Online (Sandbox Code Playgroud)

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

Ogg*_*las 6

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

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

来源:

/sf/answers/2489281721/