开发者

Web.config transformations in ASP.NET MVC 2 and extra Web.config files

i'm trying to use the Web.config transformations in my ASP.NET MVC 2 project running on .NET 4. However, I am having a problem:

// 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)" />

I keep getting this error:

Warning No element in the source document matches '/configuration/add[@name='MyDB']' C:\filePath\Web.Release.config

I narrowed this down to the Web.Config file inside the Views folder. If I give it a connectionString, such as the one in the root Web.config file, than all is well, but that means I have to maintain two Web.config files. Is there any solution to this? Am I doing something wrong?


Not sure why web.config in the views folder is being implicated but from the error you're getting it sounds like you've got a mismatch between the element in web.config and the transform config files.

In web.config, assuming <add /> is a child of <connectionStrings /> you'd do:

<?xml version="1.0" encoding="utf-8"?>

<configuration>

...

    <connectionStrings>
        <add name="SomeName" providerName="System.Data.SqlClient" connectionString="SomeConnectionString" />
    </connectionStrings>

...

</configuration>

and in 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>


As I already said: Don't forget to manually copy all the other attributes of "configuration" from the original "web.config", as it seems that VS2012 doesn't do it automatically, so there will be no match...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜