.Net Core : Read Connection String from AppSettings.json file 



Before you get into this article, if you would like to know more about ASP.NET MVC Core.

Introduction

As a developer you might have worked on read, write actions in application and database, and of course the word connection string and its properties can not even be ignored in development.

As development tool evolves, usage of features changes over a time, meaning implementation of technique changes as well.

Before ASP.NET as you know we used System.Configuration namespace to read configuration properties like connection string or key value. 

Later ASP.NET MVC Core the implementation becomes quite different, replaced System.Configuration class with IConfiguration Interface 

Tip 

Microsoft has replaced System.Configuration class with IConfiguration interface in .Net Core 2.0.


What to look first?

In this article let's explore how to read a connection string from AppSettings.json file.

Consider you have a ASP.NET Core project created in your Visual Studio 2017 or 2019. 

Your project Solution Explorer must have AppSettings.json file as below, the strong reason is it is .NET Core project. 

AppSettings.json


How to add Connection String?

Connection String always refers to secure and points to database located in local or server or AWS, Cloud Azure. It depends on the business decision what service to choose. Reading Connection String from AppSettings.json 

What I have chosen here is Azure Redis Cloud Service for this example. 

I'm sure you might be interested to know a bit what is Redis Cloud Service - Is it resource or database, how do I use it?  Alright there will an another article with source code explaining more detailed about Azure Redis Cache. 

But for now, let's take a look at AppSettings.json file

AppSettings.json Connection String


I just added two sections one is RedisCache and another Connection String.  As you noticed circled image "DemoTest" is the Redis Cache database name created in Azure Portal.

Note that I used Redis for this article, in your case it might be SQL Server, MySQL connection string but the work flow is the same.   

How to Read Connection String?

If you read above section stated, I have added two section and beneath added a connection string. It is has be navigated section wise to get access it. 

Below code explains reading it in Startup class Configuration Services class.

Reading Connection String


.NET Core simplifies to add services in IServiceCollections. And included a method called AddDistributedRedisCache which denotes it configuration service added for Redis Distributed Cache. For your case it might not be. 

You may focus only on the configuration section.

Configuration.GetSection("RedisCache").GetSection("ConnectionString").Value

Finally Value property gets the connection string stored in AppSettings.json file.

 

Summary

In this article, we have seen how to read a connection string stored in Appsetting.json file using Asp.Net MVC Core project. And hierarchical structure of having multiple section in app settings and reading in class file, explained in simple with one line of code.