How can app "know" which connection string to use based on whether it's in production or development server?

huangapple go评论63阅读模式
英文:

How can app "know" which connection string to use based on whether it's in production or development server?

问题

应用程序如何知道它当前部署在哪台服务器上,以便可以使用正确的连接字符串连接到开发/生产 SQL 服务器?我是否需要在每台服务器上设置一个环境变量,然后在启动文件中检查其值?

这是我阅读的内容,但我不确定这是否是正确的过程。

英文:

I have an ASP.NET Core app that is deployed to a dev server and prod server, sequentially (deployed to IIS).

How can the app 'know' the server that it is currently deployed to so that it can use the right connection string to connect to the dev/prod sql server? Do I have to set an environment variable on each server and have the startup file check the value of it?

This is what I've read, but am not entirely sure this is the right process.

答案1

得分: 1

你应该同时使用 appsettings.{Environment}.json 和 appsettings.json。
appsettings.{Environment}.json 中的数值将覆盖 appsettings.json 中的数值。
文件的环境版本是基于 IHostingEnvironment.EnvironmentName 加载的,该环境名称从 DOTNET_ENVIRONMENT 和 ASPNETCORE_ENVIRONMENT 环境变量中读取,您可以在每个部署中设置这些变量。
请参考 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-7.0

英文:

You should use both a appsettings.{Environment}.json and appsettings.json.
Values in the appsettings.{Environment}.json will override the appsettings.json.
The environment version of the file is loaded based on the IHostingEnvironment.EnvironmentName which reads from the DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT environment variables which you can set in each deployment.
Refer to https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-7.0

huangapple
  • 本文由 发表于 2023年7月18日 03:35:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定