简体   繁体   English

如何控制appsettings.json部署基于.Net Core环境

[英]how to control the appsettings.json deployment based on environment in .Net Core

I have ASP.Net Core 3.1 application & it has environment based appsettings.json as我有ASP.Net Core 3.1应用程序,它有基于环境的appsettings.json作为

  • appSettings.UAT.json appSettings.UAT.json
  • appSettings.Dev.json appSettings.Dev.json
  • appSettings.Prod.json appSettings.Prod.json

When the app is published/deployed to any env say UAT , all the appsettings.json gets deployed to the UAT machine.当应用程序发布/部署到任何 env 说UAT时,所有 appsettings.json 都会部署到 UAT 机器。

The requirement is that if the machine/env is UAT, then only below appSettings should be deployed要求是,如果机器/环境是 UAT,那么只应在 appSettings 下面部署

  • appSettings.json appSettings.json
  • appSettings.UAT.json appSettings.UAT.json

& if the env is Prod then & 如果环境是 Prod 那么

  • appSettings.json appSettings.json
  • appSettings.Prod.json appSettings.Prod.json

I know the other way is to store/fetch configuration from Environment variables rather appSettings.json but for now that has extra cost for me.我知道另一种方法是从环境变量而不是 appSettings.json 存储/获取配置,但现在这对我来说有额外的成本。

Thanks!谢谢!

This can be controlled by setting the ASPNETCORE_ENVIRONMENT variable on the UAT machine to UAT.这可以通过将 UAT 机器上的 ASPNETCORE_ENVIRONMENT 变量设置为 UAT 来控制。

The defaults that .net core ships with is Development, Staging and Production. .net 内核附带的默认设置是开发、登台和生产。

You can find more information from the MS Docs https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-3.1#set-the-environment您可以从 MS Docs https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-3.1#set-the-environment中找到更多信息

You can set up the "ASPNETCORE_ENVIRONMENT" variable against your website in IIS.您可以在 IIS 中针对您的网站设置"ASPNETCORE_ENVIRONMENT"变量。

Open the "Internet Information Services (IIS) Manager.打开“Internet 信息服务 (IIS) 管理器。

  • Go to the Website where you want to set the environment variable. Go 到您要设置环境变量的网站。 Find the "Configuration Editor".找到“配置编辑器”。
  • In the "Section" part of Configuration Editor, select system.webServer/aspNetCore in the left dropdown select ApplicationHost.config.在配置编辑器的“部分”部分,左侧下拉列表 select ApplicationHost.config 中的 select system.webServer/aspNetCore。
  • Click on environmentVariables then you will get the Current env variable.单击environmentVariables然后您将获得当前环境变量。 Add a new env variable.添加一个新的环境变量。
  • name will be ASPNETCORE_ENVIRONMENT and value will be Development[staging/Prod] . name将是ASPNETCORE_ENVIRONMENT并且value将是Development[staging/Prod] In your case it will be UAT or DEV or Prod在您的情况下,它将是UAT or DEV or Prod
  • Close the window and restart the website.关闭 window 并重启网站。

Give a try with the above.试试上面的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM