如何处理ASP.NET vNext调试/释放配置转换 [英] How to handle debug/release config transformations in ASP.NET vNext

查看:138
本文介绍了如何处理ASP.NET vNext调试/释放配置转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET中使用的许多我们的previous版本 Web.Debug.config / Web.Release.config 文件trasformations这将是这个样子:

In previous versions of ASP.NET many of us used Web.Debug.config/Web.Release.config files trasformations that would look something like this:

的Web.config

<connectionStrings>
  <add name="AppDB" connectionString="Data Source=(LocalDb)\\..." />
</connectionStrings>

Web.Release.config

<connectionStrings>
  <add name="AppDB" connectionString="Data Source=(ReleaseDb)\\..."  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

作为每个<一href=\"http://www.asp.net/vnext/overview/aspnet-vnext/getting-started-with-aspnet-vnext-and-visual-studio#addclasslib\">ASP.NET vNext教程你仍然可以使用的Web.config。但 config.json 似乎是现在处理配置新的方式按相同的文章:

As per ASP.NET vNext tutorial you still can use Web.config. But config.json appear to be the new way to handle configurations now as per the same article:

config.json

{
    "Data": {
        "DefaultConnection": { 
            "ConnectionString": "Server=(localdb)\\..."
        }
    }
}

而在 Startup.cs

var configuration = new Configuration();
configuration.AddJsonFile("config.json");
configuration.AddEnvironmentVariables();

所以我想知道什么是处理配置,transofrmation这一转变,以JSON建议的方法是什么?

So I'm wondering what would be the suggested way to handle config-transofrmation with this shift to json?

推荐答案

vNext采用了全新的配置系统中,你可以通读code中的环境变量。因此,在这种情况下,你会检查相应的环境变量的presence,包括通过code对应的JSON。

vNext uses a new configuration system in which you can read the environment variables through code. So, in this case you would check for the presence of the appropriate environment variable and include the corresponding JSON through code.

例如,您可以创建一个qa.json和prod.json。设置环境变量,说:ENV指向那些各自的环境质量保证和刺。然后,有条件可以添加相应的JSON。

For example, you can create a qa.json and prod.json. Set an environment variable, say, "ENV" that points to "qa" and "prod" in those respective environments. Then conditionally you can add the appropriate JSON.

在code可能是这样的:

The code could look like this:

1)default.json包含所有预设的东西。

1) default.json contains all your default stuff.

2)qa.json和prod.json包含必要的覆盖。

2) qa.json and prod.json contain the necessary overrides.

3)由于qa.json和prod.json晚一点,他们将获胜。如果在default.json和qa.json一个设置1,它会自动拿起设置1,在qa.json

3) Since qa.json and prod.json come later, they will win. If there is a "setting1" in default.json and qa.json, it will automatically pick up the "setting1" in qa.json

 var configuration = new Configuration()
                     .AddJsonFile("default.json")
                     .AddEnvironmentVariables(); 

 var envSpecificJson = configuration.Get("ENV") + ".json";
 configuration.AddJsonFile(envSpecificJson);

这篇关于如何处理ASP.NET vNext调试/释放配置转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆