Mid From PDF MVC ASP.NET Core MVC

Access in code: builder.Configuration["ConnectionStrings:Default"]; ✅ Stored securely under your user profile (not in the project folder). 🌍 5. How do you use environment variables?

Environment variables are great for overriding settings at deployment time (especially in

Docker or Azure).

Example:

set ASPNETCORE_ENVIRONMENT=Production

set

ConnectionStrings__Default="Server=mydb;Database=App;User=sa;Passwor

d=123"

Notice the double underscores __ for nested keys.

Then access in code:

var conn = builder.Configuration["ConnectionStrings:Default"];

🧭 6. What are the common environment names?

SP.NET Core defines three common hosting environments:

Environment Purpose

Developmen

Local development, detailed errors, hot reload

Staging Pre-production testing

Production Live environment, performance optimized, no detailed errors

You set the environment via:

set ASPNETCORE_ENVIRONMENT=Development

🔍 7. How do you detect the current environment?

You can inject or access the IWebHostEnvironment or IHostEnvironment service.

Example:

public class HomeController : Controller
{
private readonly IWebHostEnvironment _env;
public HomeController(IWebHostEnvironment env)
{
_env = env;
}
public IActionResult Index()
{
if (_env.IsDevelopment())
return Content("Running in Development mode");
return Content($"Environment: {_env.EnvironmentName}");
}
}

You can also access it in Program.cs:

if (builder.Environment.IsProduction())
{

// Configure production services

}

🧾 8. How to use different appsettings.json files?

ASP.NET Core supports environment-specific JSON files.

Example structure:

ppsettings.json

ppsettings.Development.json

ppsettings.Staging.json

ppsettings.Production.json

Program.cs:

builder.Configuration

.AddJsonFile("appsettings.json")

.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.jso

n", optional: true);

t runtime, only the file matching the current environment will override base settings.

Example:

ppsettings.json

{ "AppName": "MyApp", "LogLevel": "Information" }

ppsettings.Production.json

{ "LogLevel": "Error" }

Result in Production → LogLevel = Error.

🔄 9. How to reload configuration dynamically?

You can make JSON configuration files auto-reload when changed.

Example:

builder.Configuration.AddJsonFile("appsettings.json", optional:

false, reloadOnChange: true);

If you update appsettings.json, the new values are reflected automatically in your app

without restarting.

You can subscribe to changes using IOptionsMonitor (see below).

📦 10. How to bind configuration to POCO classes?

You can map sections of your configuration directly to C# classes (POCOs).

Example:

ppsettings.json

{

"AppSettings": {

"SiteName": "TechStore",

"PageSize": 20,

"EnableCache": true

}
}

Create a POCO:

public class AppSettings
{
public string SiteName { get; set; }
public int PageSize { get; set; }
public bool EnableCache { get; set; }
}

Bind configuration:

builder.Services.Configure<AppSettings>(

builder.Configuration.GetSection("AppSettings"));

Inject it into a controller:

public class HomeController : Controller
{
private readonly AppSettings _settings;
public HomeController(IOptions<AppSettings> options)
{
_settings = options.Value;
}
public IActionResult Index()
{
return Content($"Welcome to {_settings.SiteName}!");
}
}

You can also use IOptionsSnapshot for per-request reload or IOptionsMonitor for live

updates.

Web APIs & REST

More from ASP.NET Core MVC Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details