ASP.NET MVC3:调试和发布应用程序设置不起作用

Ali*_*air 27 asp.net-mvc web-config

我的调试和发布web.config应用程序设置未正确读取.

Web.config文件:

<appSettings>
 <add key="webpages:Version" value="1.0.0.0"/>
 <add key="ClientValidationEnabled" value="true"/>
 <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)

Web.Debug.config

<appSettings>
    <add key="ErrorEmailAddress" value="developerEmail@email.com" />
    <add key="TestModeEmailAddress" value="developerEmail@email.com" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

Web.Release.config

<appSettings>
    <add key="ErrorEmailAddress" value="prodErrorEmail@email.com" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

但是,致电:

WebConfigurationManager.AppSettings["ErrorEmailAddress"]
Run Code Online (Sandbox Code Playgroud)

返回null(调试时).

我试过添加xdt:Transform ="Insert"例如

<add key="ErrorEmailAddress" value="prodErroEmail@email.com" xdt:Transform="Insert" />
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Ali*_*air 47

好吧我明白了.

这里回答:如何在内置的visual studio调试器服务器中使用Web.debug.config?

因此,配置文件仅在发布时合并,而不是在对本地服务器运行时合并.相当愚蠢的IMO,还有什么时候你会使用Web.Debug.config?

我将按照此处的建议执行操作:使用Visual Studio web.config转换进行调试

只需将Web.config作为我的默认调试配置文件,然后在发布时发布.在这一点上看不到Web.Debug.config的用途.

尽管如此,这很烦人,因为我的大多数设置都希望为所有环境设置一种方式,但在开发时(例如customErrors On).这意味着我必须在Web.config中设置它们以进行调试,然后在我的所有其他环境中配置更改它们.

谢谢大家的回复.


小智 10

<!-- Web.Config -->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings configSource="AppSettings.config" />
</configuration>

<!-- AppSettings.config -->
<appSettings>
<add key="MyDoe" value="Arnold" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>

<!-- Web.Release.Config -->
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<compilation xdt:Transform="RemoveAttributes(debug)" />
<appSettings>
<add key="MyDoe" value="John" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)


Mus*_*gdy 7

我发现了,

首先,您将使用空值或调试值在web.Config的appSetting上添加应用程序设置条目

<add key="Environment" value="Localhost" />
Run Code Online (Sandbox Code Playgroud)

然后在web.release.config上添加相同的不同值,但添加转换部分

 <add key="Environment" value="DifferentValue"  xdt:Transform="Replace" xdt:Locator="Match(key)"/>
Run Code Online (Sandbox Code Playgroud)

然后,当您在发布模式下发布网站时,您将获得发布值,您也可以将其添加到调试配置中,然后在调试配置中使用不同的值发布