Read Web.Config Configuration .NET Core
In this article, we shall see how to Read Web.Config Configuration .NET Core-based applications like ASP.NET Core or MVC or non-host applications.
Today in this article, we will cover below aspects,
Web.Config is actually a useful and most widely used format used for providing configurations details required for an application to be able to run properly.
However, this format is no longer seems to work in NET Core ecosystems.
In .NET Core preferred format is appsettings.json or you can very much use .xml or .ini or any other .json file format etc. It does support multiple file format.
Please visit the below article on the guidelines,
Can I still use web.config in .NET Core?
You might still be having a question if you can still use web.config in .NET Core-based application.
The short answer is Yes but you just need to rename your file as app.config and it works fine simply!
I found .NET Core has support for every type of configuration source and lets you load configuration from XML or JSON or INI or other config files etc.
Getting started
Let’s create in ASP.NET Core API.
Please install below Nuget package below using the Package Manager console or Package Manager in Visual Studio,
PM> Install-Package System.Configuration.ConfigurationManager -Version 4.7.0
Below is the sample app.config file,
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="MYDBCON" connectionString="server=/SQL;integrated security=true;database= your database name" />
</connectionStrings>
</configuration>
With the above provision, you can very much use the traditional web.config files for loading configuration details by renaming.
Similar way above technique can be used in Console applications or Library projects if any.
CreateDefaultBuilder in NET Core
CreateDefaultBuilder is a generic Host Builder with the added ability to load configuration in the preferred format of the below types etc in .NET and ASP.NET Core.
This host builder plays an important role in initializing the Host and initializing the configuration with ease like getting access to the application’s host details including app configuration, logger configuration, user secretes, getting access to environmental configuration, etc.
Please visit this article for more information,
That’s All!
Did the above technique help you resolve the issue? Do you have any other better way to deal with it?
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.
What if you have to have 2 connection string or app settings in your app?
Then host builder will throw an exception “‘A duplicate key ‘appSettings:add:key’ was found. ”
Usually we a re not limited to just one setting or a connection string. Is there a workaround?
Hello Val- Thanks for your query. As per my understanding both apsettings.json and app.config can co-exist in the application. You can use IConfiguration configuration to inject json,ini,xml and You can use app.config as an old legacy way to specify connection string.