Header add

coreprogram

In this article we discuss How to Read Connection String inside Startup.cs from AppSettings.json file in ASP .NET Core. Before starting this article please go through our previous article How to read values from appsettings.json in Asp.Net Core

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

What is IConfiguration ?
  • The IConfiguration is an interface for .Net Core.
  • The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller.
  • The IConfiguration interface is used to read Settings and Connection Strings from AppSettings.json file.



 ➥ Open VS ⟹ Choose ASP .NET Core Web Application ⟹ Choose Empty template

Fig-1.1
Fig-1.2
When you choose as empty template it create the basic templates where the appestiings.json, startup and program files are found.





➥ Open the appsettings.json file add the connection string like below;


➥ Reading Connection String from AppSettings.json file inside Startup class

In the below example, the IConfiguration interface is injected in the Startup class and assigned to the private property Configuration. Then inside the ConfigureServices method, the Connection String is read from the AppSettings.json file using the GetConnectionString function.

When we run the application you can see in the below image the connection string is read form appsettings.json file as expected.

Fig-2.1




</> Find the Source Code in Github.com/CoreProgramm/
    Summary
In this tutorial we discussed how to Read Connection String inside Startup.cs from AppSettings.json file in ASP .NET Core. If have any question related to this topic then give your feedback.

You May Also Like...

Post a Comment

Previous Post Next Post