Web.config no longer exists in .NET Core
I thought to write a post about my journey making projects with .NET Core but that will be a long post and maybe nobody will read it; instead, I will make a series of posts with some topics I found interesting to talk about.
Many of us come from ASP.NET 4 and old versions, we used to work with Web.config in our web projects, a file to place our settings in XML format, but it no longer exists in .NET Core (or at least in our Solution Explorer).
In .NET Core we use appsettings.json to place our settings in JSON format. Let's see how this works by getting our hands dirty:
In .NET Core we use appsettings.json to place our settings in JSON format. Let's see how this works by getting our hands dirty:
1) Create a .NET Core Web Application project:
You will see a bunch of files generated from the template in the Solution Explorer. appsettings.json is meant to replace settings previously located in Web.config . To catch these configurations in the project, the Startup.cs file has the following lines:
As you can see the appsettings.json file is added to the ConfigurationBuilder. Also, pay attention to the $"appsettings.{env.EnvironmentName}.json" line, where it checks the environment, i will talk about that in a few minutes.
2) To add application settings to our application, open appsettings.json and add the following lines before the "Logging" part already there:
"AppSettings": {
"Spoiler": "If you watch Game Of Thrones you are awesome"
},
For the record, both AppSettings and Spoiler could have been named anything we’d like, this is just an example.
3) .NET Core uses an options pattern to resolve settings from appsettings.json. To read the Spoiler setting add the following line to the ConfigureServices method in Startup.cs:
var appSettings = Configuration.GetSection("AppSettings");
4) Another point to keep in mind .Net Core is based on dependency injection, you will be injecting various options in your controllers. To register AppSettings in the container, create a new C# class to represent the settings:
public class AppSettings
{
public string Hello { get; set; }
}
5) Now register the service in the Startup.cs file just after the call to GetSection (step 3):
services.Configure<AppSettings>(appSettings);
6) Now it's time to inject it into our controller. Add a constructor to the HomeController looking like this:
private readonly AppSettings _appSettings;
public HomeController(IOptions<AppSettings> appSettings)
{
_appSettings = appSettings.Value;
}
7) To use the Spoiler option from our settings, we’ll send the value in the Index method:
public IActionResult Index()
{
ViewBag.Spoiler = _appSettings.Spoiler;
return View();
}
8) Finally in our Index.cshml:
<h1> This is very important: @ViewBag.Spoiler</h1>
Press F5 And Voila!
AppSetings enviroments
As you can remember in our ASP.NET 4 project, We had different web.config files depending on the deploying environment we wanted to use:
We used to have settings for the development and production environment in most cases. Now in .Net Core we have something similar. Let's create another set of settings for the production environment, create a new JSON file named appsettings.Production.json .See how the new file is automatically nested beneath appsettings.json
To override the value of the Spoiler setting, add the following to appsettings.Production.json:
{
"AppSettings": {
"Spoiler": "If you watch Game Of Thrones you are awesome and also you are in Production"
}
}
Open launchSettings.json file and change "ASPNETCORE_ENVIRONMENT": "Development" for "ASPNETCORE_ENVIRONMENT": "Production"
Press F5
It knows the environment thanks to this line in the Startup.cs :
appsettings.{env.EnvironmentName}.json
If you have any question leave a comment in the section below, I'll get to it as soon as possible.