Wednesday, October 5, 2022
HomeError SolveEnvironment Variable name in Dotnet Core

Environment Variable name in Dotnet Core

What is Environment Variable in Dotnet Core?

Developers uses various connection string while working in the developing field. For example,

  1. Localhost Connection String
  2. Test Server Connection String
  3. Production Server Connection String

As professionals, we have to set up the connection string once and based on our environment (Like Production OR Test OR Local) use them to perform our CRUD operation.

In this blog post, we are going to set up the environment variable name in Dotnet core and use them in our
1) Controller
2) .cshtml Page.

How to set environment variable Dotnet core ?

  1. Open your project.
  2. Find Properties folder, in which launchSettings.json file is placed.
  3. Open launchSettings.json file

Here on this page, you can see the below code.

 "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development" //Production , Development,Staging
      }

You can set the environment variable Like “Production, Development, Staging”.

How to setup connection string based on environment variable?

Open appsetting.json file.

  "ConnectionStrings": {
    "LiveProjectAppDB": "YOUR_LIVE_SERVER_CONNECTION_STRING",
    "TestLiveProjectAppDB": "YOUR_LIVE_TEST_SERVER_CONNECTION_STRING",
    "LocalDebugLivePRojectAppDB": "YOUR_LOCALHOST_CONNECTION_STRING"
  },

Open Startup.cs File

public Startup(IConfiguration configuration, IWebHostEnvironment env)
        {
            Configuration = configuration;
            _env = env;
        }
public void ConfigureServices(IServiceCollection services)
        {
            string dbConnect;
            if (_env.IsProduction())
            {
                //Production
                dbConnect = Configuration.GetConnectionString("LiveProjectAppDB");
            }
            else if (_env.IsStaging())
            {
                //Staging
                dbConnect = Configuration.GetConnectionString("TestLiveProjectAppDB");
            }
            else
            {
                //Development
                dbConnect = Configuration.GetConnectionString("LocalDebugLivePRojectAppDB");
            }
}

How to display environment variables in _Layout.cshtml file?

  1. If you want to display IN YOUR HEADER then open _Layout.cshtml File.
<environment names="Production">
<a class="navbar-brand" asp-area="" asp-controller="" asp-action="">YOUR PROJECT NAME - Production</a>
</environment>
<environment names="Development">
<a class="navbar-brand" asp-area="" asp-controller="" asp-action="">YOUR PROJECT NAME - Development</a>
</environment>
<environment names="Staging">
<a class="navbar-brand" asp-area="" asp-controller="" asp-action="">YOUR PROJECT NAME - Staging</a>
</environment>

How to display environment variables in view dotnet core?

@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnv
 <input type="text" value="@hostingEnv.EnvironmentName" name="EnvironmentName" id="EnvironmentName"/>

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments