Use Multiple Environments in ASP.NET Core

Introduction

Each application has a different configuration for each environment in the real-world application. In this article, we will explore the process of setting up configurations for individual environments and deploying the application to ensure it accesses the appropriate configuration settings.

We will cover,

  1. How to configure configuration for each environment with example.
  2. How to deploy on the different environments with example.

Let’s follow the below steps to create the. NET Core MVC application.

Step 1. Create an MVC application with the name "EachConfig"

EachConfig

Step 2. Now, we will add a few configurations in the appsetting.json file.

Configurations

Step 3. Let’s add the below configuration in the Development file.

 Development file

Step 4. Now add a new appsetting file for UAT and Production environments.

 UAT

Production file

Production file

Step 6. Add the below code in the HomeController file.

using EachConfig.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace EachConfig.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IConfiguration _config;
        public HomeController(ILogger<HomeController> logger, IConfiguration config)
        {
            _logger = logger;
            _config = config;
        }

        public IActionResult Index()
        {
            var configName = _config.GetSection("Configuration").GetValue<string>("Name");
            ViewData["Name"] = configName;
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

In the above code

  1. Inject IConfiguration in the HomeController.
  2. Read Configuration value in the Home – Index Action method

Step 7. Read ViewData Value in the Index. cshtml.

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome :  @Convert.ToString(ViewData["Name"]); </h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

Step 8. Run the application and see the output.

Run the application

Step 9. Now we will inspect the launch. JSON file.

Index

We observed in the above code that “ASPNETCORE_ENVIROEMENT” has the value “Development” and “appsettings.Development.json” called.

It concludes that if environment value is used to pick the correct appsetting file.

Let’s try to change “ASPNETCORE_ENVIROEMENT” to “UAT” and see the output.

Step 10. Change the value to “UAT” in the “ASPNETCORE_ENVIROEMENT”.

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:62803",
      "sslPort": 44375
    }
  },
  "profiles": {
    "EachConfig": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7198;http://localhost:5245",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "UAT"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Now we will execute the application and see the output.

Output

Wow! It’s working and it has taken the “appsetting.UAT.json” file.

Now, a question arises as to how we will deploy on the production.

How to set up an environment variable?

To pick the correct configuration, we need to set the Environment Variable with the correct Value.

Follow the below steps to set up the Environment variable in the server.

 Environment

Create a New Environment Variable.

Environment Variable

Once the variable is configured on the server, the application will automatically reflect the corresponding "app settings. {Environment}.json" file.

That’s all for this article, hope you find this article useful.


Similar Articles