ASP.NET Core Configurations settings
The ASP.NET Core Configurations settings are configured on 3 files:
1. The Project File also known as “.csproj” file.
2. Program.cs
3. appsettings.json
These configuration settings tell the ASP.NET Core app how it should work based on the users interaction
What is .csproj Project File
The “.csproj” is the Project file of an ASP.NET Core project. It contains name of the SDK, ASP.NET Core Version, references to Packages which the project is using, and many such things. The Project .csproj file is named as NameofProject.csproj.
This file is hidden by Visual Studio and can be accessed by right clicking the project on Solution Explorer and selecting 'Edit projectname.csproj' / Edit Project File (Depends on Version of Visual Studio).
The csproj can be used to install a NuGet Package to your ASP.NET Core project. You simply have to reference the package to the csproj file and Visual Studio will downloads that package for you. Similarly when you remove any package refrence from the csproj file then Visual Studio removes that package from your application. As soon as you save the csproj file, VS will download this package from NuGet and install it to your project.
What is Program.cs File -
The Program.cs is used to register the different services on the app so that they can be used. Some of the commonly used predefined services which we register in this class are:
AddControllersWithViews – which adds services for controllers, API-related features, and views in the application.
AddRazorPages – which adds services for Razor Pages.
AddDbContext – registers a DB Context which is commonly Entity Framework Core services.
AddDefaultIdentity – registers Identity services to the application.
In Program.cs we also configure HTTP Request pipeline for the ASP.NET Core app. The request pipeline is configured by adding middleware components. The most common ones are:
UseDeveloperExceptionPage – adds the “ExceptionHandler” middleware that will show developer exception page with detailed information whenever unhandled request exceptions arises in the app. It should be used only when the app is running in the Development environment.
UseExceptionHandler – adds “ExceptionHandler” middleware to catch exceptions and logs them. It should be used in Production environment i.e. when the app is running in Production.
UseHsts – adds HSTS Middleware which enables HSTS header. HSTS full form is (HTTP Strict Transport Security).
UseHttpsRedirection – adds HTTPS Redirection Middleware to enforce HTTPS by redirecting all HTTP requests to HTTPS.
UseStaticFiles – adds Static File Middleware which enables static files to be served.
UseRouting – adds Routing middleware which matches request to an endpoint.
UseAuthorization – adds Authorization middleware.
UseAuthentication – adds Authentication middeware.
UseEndpoints – adds Endpoint Routing Middleware to execute the matched endpoint.
MapControllerRoute – adds Routing middleware to match the URLs of incoming requests and map them to actions methods.
What is appsettings.json File
The appsettings.json contains keys and values for storing database connection strings, global variables and other configuration values for an ASP.NET Core application. The appsettings.json file lies on the root folder of the application.
You can create this file by right clicking the project name in the Solution Explorer and selecting Add ➤ New Item. In the ‘Add New Item’ dialog box, search appsettings.json
Let say we added few settings in appsettings.json file
In Program class we can read the configuration by app.Configuration() method. So add the following conditional code in program class that will read the configurations from appsettings and register the middlewares. Lets read the bool values of these 4 keys.
Access appsettings.json in Controller
We can access appsettings.json in the Controller by Injecting IConfiguration to the constructor of the Controller
In large applications there might be hundreds of entries in the appsettings.json so it does not make sense for the controller to get access to every key-value entries.
A good approach would be to extract the needed section (i.e. key-values) from appsettings.json, convert it to a class then use this class to access those key-value
1. Create the class to contain the appsettings.json section values.
2. Register this class with Configure<T> on the Program class
3. Retrive the class on the Controller with IOptions<T>.