asp.net-mvc ASP.NET MVC3:调试和发布应用设置不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6209125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:14:11  来源:igfitidea点击:

ASP.NET MVC3: Debug and Release app settings not working

asp.net-mvcweb-config

提问by Alistair

My debug and release web.config app settings are not being read correctly.

我的调试和发布 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>

Web.Debug.config

网页调试配置

<appSettings>
    <add key="ErrorEmailAddress" value="[email protected]" />
    <add key="TestModeEmailAddress" value="[email protected]" />
</appSettings>

Web.Release.config

Web.Release.config

<appSettings>
    <add key="ErrorEmailAddress" value="[email protected]" />
</appSettings>

However, calling:

但是,调用:

WebConfigurationManager.AppSettings["ErrorEmailAddress"]

is returning null (when debugging).

返回 null(调试时)。

I have tried adding xdt:Transform="Insert" e.g.

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

<add key="ErrorEmailAddress" value="[email protected]" xdt:Transform="Insert" />

Any ideas?

有任何想法吗?

回答by Alistair

Ok I figured it out.

好吧,我想通了。

Answered here: How can I use Web.debug.config in the built-in visual studio debugger server?

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

So the config files are only combined when you publish, not when you are running against a local server. Pretty stupid IMO, when else would you ever use Web.Debug.config?

因此,配置文件仅在您发布时合并,而不是在本地服务器上运行时合并。非常愚蠢的国际海事组织,你还会在什么时候使用 Web.Debug.config?

I will do as is suggested here: Use Visual Studio web.config transform for debugging

我会按照这里的建议去做:使用 Visual Studio web.config 转换进行调试

and just have Web.config as my default debugging config file, then have release for when releasing. Can't see a use for Web.Debug.config as this point.

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

Still, this is annoying because most of my settings I want to be set one way for all environments but when developing (eg customErrors On). This means I have to set them in Web.config for debugging, then in all my other environment configs change them.

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

Thanks everyone for responses.

谢谢大家的回复。

回答by Bjoern

<!-- 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>

回答by Mustafa Magdy

I found it out,

我发现了,

first you will add the app settings entries on appSetting of the web.Config with empty values or with debug values

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

<add key="Environment" value="Localhost" />

then you add the the same with the different values on the web.release.configbut add the transformation part

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

 <add key="Environment" value="DifferentValue"  xdt:Transform="Replace" xdt:Locator="Match(key)"/>

Then when you publish the website on release mode you'll get the release values, You can also add the same to debug config then publish in debug config with different values

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

回答by RPM1984

I've never had it working with not having the key in the default web.config.

我从来没有在默认 web.config 中没有密钥的情况下使用过它。

This works for me:

这对我有用:

Web.config

网页配置

<add key="Environment" value="Localhost" />

Web.Debug.config

网页调试配置

<add key="Environment" value="Development" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>

Web.Release.config

Web.Release.config

<add key="Environment" value="Production" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>

回答by mymex1

Could you maybe post all of your web.configs? Default, debug, and release? One way to test if it's working is maybe set something like different connection strings for debug and release and check which one it uses when your app is running.

你能不能把你所有的 web.configs 都贴出来?默认、调试和发布?测试它是否正常工作的一种方法是为调试和发布设置不同的连接字符串之类的东西,并检查它在您的应用程序运行时使用哪一个。

回答by m4tt1mus

are you debugging in release mode? in the toolbar next to the green arrow used to start debugging you can set a mode, make sure it isn't on release.

你是在发布模式下调试吗?在用于开始调试的绿色箭头旁边的工具栏中,您可以设置一种模式,确保它不在发布中。