Friday, 31 December 2021

Access Connection String In Asp.Net Core 6 ClassLibrary From appsettings.json C# | appsettings.json Connection String Value Access in Asp.net Core ClassLibrary

12/31/2021 - By Pranav Singh 0

 In this article I will show you how you can access connection string from appsetting.json in a class library class file using c#.net in your asp.net core 6 web application or asp.net core mvc application.  So for this first we will add the connection string in your appsetting.json file.


"ConnectionStrings": {

    "grattxpertconnection": "Server=.\\SQLEXPRESS;Database=TestDB;Trusted_Connection=True;”

  }

 Related Articles:

After this we will add a class library project. In this class library we will add a new class file. We will make the added class file as static. Now add a function on which we will access the connection string in our class library.

But before writing the core we need to add the library reference of  Microsoft.Extensions.Configuration.Json and Microsoft.Extensions.Configuration.Binder. For this we will right click on project and click on manager nuget package.  Add search for  Microsoft.Extensions.Configuration.Json and Microsoft.Extensions.Configuration.Binder and install it.


Microsoft.Extensions.Configuration.Json


Microsoft.Extensions.Configuration.Json


Microsoft.Extensions.Configuration.Binder

Microsoft.Extensions.Configuration.Binder

After this we will go to our class file and add the below code.


using Microsoft.Extensions.Configuration;

using System;

using System.IO; 

namespace ClassLibrary1

{

    public class Class1

    {

        public static string Connectionstring()

        {

            var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: false);

            IConfiguration config = builder.Build();

            return config.GetValue<string>("ConnectionStrings:value");

        }

    }

}

 

In above code I have accessed the appsetting.json file and the read the data for specific configuration from appsetting.json file on asp.net core we application. Now we will add the library reference in the project where we want to access the appsetting.json defined connection string.

Now lets access this in our project.



 

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top