In this article we discuss How to read connection string 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.
➤ 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 |
➥ 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
Post a Comment