If you have worked with .NET before Core was released, you will be familiar with the App.config file. It is an XML settings file that was later replaced with JSON in .NET Core applications. If you still harbour a likeness to the XML App.config file, you will discover that it is still quite easy to use it for configuration settings instead of its JSON counterpart. In this article, we will read App.config settings from a .NET 8.0 console application and an ASP.NET 8.0 Razor Pages Application.
Companion Video: https://youtu.be/a_U4qNohkh0
Setup Console Application
In a suitable working folder, create a console application named ReadAppConfig with the following terminal window command:
dotnet new console -o ReadAppConfig
Change into the ReadingAppConfig folder with:
cd ReadAppConfig
Install the ConfigurationManager package with the following command:
dotnet add package System.Configuration.ConfigurationManager
The App.config file in Console Application
Create a file named App.config in the root of your application and add to it the following XML content:
<?xml version="1.0"?><configuration><appSettings><add key="endpoint" value="https://endpoint.somewhere.com/" /></appSettings><connectionStrings><addname="sqlServer"providerName="System.Data.SqlClient"connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=True;" /></connectionStrings></configuration>
The above file contains the following important settings:
- an application setting named "endpoint" with value "https://endpoint.somewhere.com/"
- a database connection string named "sqlServer" with value"Data Source=localhost;Initial Catalog=MyDB;Integrated Security=True;"
Program.cs in Console Application
Replace the contents of Program.cs with the following C# code:
using System.Configuration;
string _endpoint = ConfigurationManager.AppSettings["endpoint"]!;string _connectionString = ConfigurationManager.ConnectionStrings["sqlServer"]!.ConnectionString;
Console.WriteLine($"Endpoint: {_endpoint}");Console.WriteLine($"Connection String: {_connectionString}");
Run Console Application
In a terminal window, run the app with the following command:
dotnet run
You should see the following output that verifies that information from App.config has successfully been read:
Endpoint: https://endpoint.somewhere.com/Connection String: Data Source=localhost;Initial Catalog=MyDB;Integrated Security=True;
ASP.NET Razor Pages Application
Next, let us do the same thing in an ASP.NET Razor Pages Application.
Exit the previous console application and create a razor pages application is a separate folder with:
dotnet new razor -o ReadAppConfigWeb
Change to the folder containing the Razor Pages app with:
cd ReadAppConfigWeb
Add the ConfigurationManager package with:
dotnet add package System.Configuration.ConfigurationManager
In the root folder of your ASP.NET Razor Pages application, add the same App.config file as before:
<?xml version="1.0"?><configuration><appSettings><add key="endpoint" value="https://endpoint.somewhere.com/" /></appSettings><connectionStrings><addname="sqlServer"providerName="System.Data.SqlClient"connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=True;" /></connectionStrings></configuration>
Open Pages/Index.cshtml.cs in your favourite editor and make the following changes to it:
1) Import System.Configuration with:
using Config = System.Configuration;
2) Add the following instance variables to the IndexModel class:
string _endpoint = Config.ConfigurationManager.AppSettings["endpoint"]!;
string _connectionString = Config.ConfigurationManager.ConnectionStrings["sqlServer"]!.ConnectionString;
3) Append these lines of code to the constructor:
_logger.LogInformation($"Endpoint: {_endpoint}");
_logger.LogInformation($"Connection String: {_connectionString}");
Now we can run the application with:
dotnet watch
In the terminal window you will notice the following output, which shows that the setting have indeed been read from App.config:
Endpoint: https://endpoint.somewhere.com.com/
Connection String: Data Source=localhost;Initial Catalog=MyDB;Integrated Security=True;
Conclusion
When using .NET Core, you can always resort to saving your configuration setting in an XML file instead of JSON.
No comments:
Post a Comment