Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Web.config获取密钥的转换值_C#_Web Config_Web.config Transform - Fatal编程技术网

C# 如何从Web.config获取密钥的转换值

C# 如何从Web.config获取密钥的转换值,c#,web-config,web.config-transform,C#,Web Config,Web.config Transform,我的Web.config文件中有以下密钥: <appSettings> <add key="ImageBucketName" value="dev" /> </appSettings> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="ImageBuck

我的
Web.config
文件中有以下密钥:

<appSettings>
    <add key="ImageBucketName" value="dev" />
</appSettings>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="ImageBucketName" value="live" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
</configuration>
但是,当我从Visual Studio 2013运行应用程序并尝试使用控制器从中检索密钥的值时,我总是得到未转换版本的密钥

// Get the AWS bucket name from the config file
var imageBucketName = ConfigurationManager.AppSettings["ImageBucketName"];
与以下结果相同:

// Get the AWS bucket name from the config file
var imageBucketName = WebConfigurationManager.AppSettings["ImageBucketName"];

当我以“Release”的形式运行应用程序时,如何确保我获得了正确的版本的密钥?

当直接在Visual Studio中运行时,将不会应用转换-您至少需要首先部署到某个地方(即使在本地)

基本原理是适用于不同环境的转换。基本web.config文件表示本地(dev)环境,而
.release
转换将适用于产品(或类似产品)环境


如果您只想查看转换的运行情况,一种简单的方法是通过命令行XDT工具运行转换,该工具可从

中获得,您的输出文件夹中的web.config文件是什么样子的(对于web项目,通常是
\bin
)?对不起,我可以。我现在正在查看创建的web.config显示了未转换的版本,因此返回“dev”,并且您肯定已将配置设置为“Release”()?是的。它似乎只有在我执行实际的“部署”时才会改变,而不仅仅是通过从工具栏设置“发布/调试”设置