【问题标题】:Web.config transformations in ASP.NET MVC 2 and extra Web.config filesASP.NET MVC 2 和额外的 Web.config 文件中的 Web.config 转换
【发布时间】:2011-05-29 05:39:03
【问题描述】:

我正在尝试在 .NET 4 上运行的 ASP.NET MVC 2 项目中使用 Web.config 转换。但是,我遇到了一个问题:

// Root Web.config
<add name="MyDB" connectionString="default...default" />


// Root Web.Debug.config
<add name="MyDB" connectionString="debug...debug" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />

// Root Web.Release.config
<add name="MyDB" connectionString="release... release" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />

我不断收到此错误:

警告源文档中没有元素匹配 '/configuration/add[@name='MyDB']' C:\filePath\Web.Release.config

我将其缩小到 Views 文件夹中的 Web.Config 文件。如果我给它一个connectionString,例如根Web.config 文件中的那个,那么一切都很好,但这意味着我必须维护两个Web.config 文件。有什么解决办法吗?我做错了吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2 web-config transformation


    【解决方案1】:

    不知道为什么会涉及视图文件夹中的 web.config,但从您收到的错误来看,您似乎在 web.config 中的元素和转换配置文件之间存在不匹配。

    在 web.config 中,假设 &lt;add /&gt;&lt;connectionStrings /&gt; 的孩子,你会这样做:

    <?xml version="1.0" encoding="utf-8"?>
    
    <configuration>
    
    ...
    
        <connectionStrings>
            <add name="SomeName" providerName="System.Data.SqlClient" connectionString="SomeConnectionString" />
        </connectionStrings>
    
    ...
    
    </configuration>
    

    在 web.debug.config 中

    <?xml version="1.0" encoding="utf-8"?>
    
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    
    ...
    
        <connectionStrings>
            <add xdt:Locator="Match(name)" xdt:Transform="SetAttributes(connectionString)" name="SomeName" connectionString="SomeOtherConnectionString" />
        </connectionStrings>
    
    ...
    
    </configuration>
    

    【讨论】:

    • 好的,在 [SetAttributes(connectionString)] 中添加括号不起作用并引发错误。但是,我注意到我的问题是我没有在调试/发布配置文件中用 包装 。当我看到你的例子时,我突然意识到这一点。所以+1给你一个例子。 :)
    【解决方案2】:

    正如我已经说过的: 不要忘记手动从原来的“web.config”中复制“配置”的所有其他属性,因为VS2012似乎不会自动这样做,所以不会匹配......

    【讨论】:

      猜你喜欢
      • 2010-10-05
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多