更改环境变量运行时间
如何更改环境变量运行时间? 使用登录页面我正在选择开发或生产。 基于该,我必须专门使用ConnectionsTring设备。
"ConnectionStrings": {
"PROJECTNAME_Development": "SERVER_DETAILS",
"PROJECTNAME_Production": "SERVER_DETAILS",
}
这是我的loginservices.cs文件
public class LoginService: ILoginService
{
private readonly string _connectionString = string.Empty;
private readonly IConfiguration _configuration;
}
public LoginService (IConfiguration configuration)
{
_configuration = configuration;
//If ADMIN SELECT Development
_connectionString = _configuration.GetConnectionString("PROJECTNAME_Development");
//If ADMIN SELECT Production
_connectionString = _configuration.GetConnectionString("PROJECTNAME_Production");
}
更新1 我尝试了此代码。 它可以正常工作,但是还有其他方法,例如在一个位置我进行更改,环境变量值将更新。
我的控制器
[HttpPost]
public IActionResult Login(LoginModel model)
{
try
{
if (ModelState.IsValid)
{
model.DevelopmentServer = "Development";
// model.DevelopmentServer = "Production";
...
var Result = ILoginService.AdminLogin(model);
public LoginService(IConfiguration configuration, IOptions<ConnectionStringDetails> connectionStrings)
{
_proDBCon = connectionStrings.Value.PROJECTNAME_Development;
_proDBConProduction = connectionStrings.Value.PROJECTNAME_Production;
}
在同一文件loginservice.cs文件中
public LoginInfo AdminLogin(LoginModel model)
{
if (model.DevelopmentServer == "Development") {
_connectionString = _proDBCon;
}
else {
_connectionString = _proDBConProduction;
}
}
How can I change the environment variable RUN TIME?
Using Login PAGE I am selecting Development or Production.
Based on that I have to use my ConnectionString device specifically.
"ConnectionStrings": {
"PROJECTNAME_Development": "SERVER_DETAILS",
"PROJECTNAME_Production": "SERVER_DETAILS",
}
Here are my LoginServices.CS file
public class LoginService: ILoginService
{
private readonly string _connectionString = string.Empty;
private readonly IConfiguration _configuration;
}
public LoginService (IConfiguration configuration)
{
_configuration = configuration;
//If ADMIN SELECT Development
_connectionString = _configuration.GetConnectionString("PROJECTNAME_Development");
//If ADMIN SELECT Production
_connectionString = _configuration.GetConnectionString("PROJECTNAME_Production");
}
UPDATE 1
I tried this code. It works fine but is there any other way like at one location I make a change and Environment Variable value gets updated.
My Controller
[HttpPost]
public IActionResult Login(LoginModel model)
{
try
{
if (ModelState.IsValid)
{
model.DevelopmentServer = "Development";
// model.DevelopmentServer = "Production";
...
var Result = ILoginService.AdminLogin(model);
public LoginService(IConfiguration configuration, IOptions<ConnectionStringDetails> connectionStrings)
{
_proDBCon = connectionStrings.Value.PROJECTNAME_Development;
_proDBConProduction = connectionStrings.Value.PROJECTNAME_Production;
}
In the same file LoginService.cs File
public LoginInfo AdminLogin(LoginModel model)
{
if (model.DevelopmentServer == "Development") {
_connectionString = _proDBCon;
}
else {
_connectionString = _proDBConProduction;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您需要不惜一切代价从UI中切换两个数据库,则您将无法使用容器/注射完全完成它。您必须使用从UI传递的值来确定应使用哪个数据库连接字符串。
同时,我将考虑部署两个应用程序的两个单独的实例,一个为开发人员配置,另一个为prod配置。这样,您将获得更多开箱即用的(注射容器和环境配置)。
Assuming that you need to switch between two databases from UI at any cost, you won't be able to complete it entirely using containers/injections. You have to use the value passed from UI to determine which database connection string should be used.
Meanwhile, I would consider deploying two separate instances of an application, one configured for dev and another for prod. In such way, you would get more out of the box (injections container and environment configuration).