ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • How to Upload File in ASP.NET Core with ViewModel?
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

How to read appSettings JSON from Class Library in ASP.NET Core

.Net .Net Core about 7 years ago || 12/17/2018 || 46.4 K View

  • Hide

Do you have a problem on accessing appsetting.JSON file of the main project From Class library?

Below I have provided one of the procedure on Getting Appsetting value.

Let assume We create the project on .net core web application and Added the Class Library. In my Case project structure looks like this.

Class library

 

  1. Install the following Nuget Packege in Class library
  • Microsoft.Extensions.Configuration
  •  Microsoft.Extensions.Configuration.Abstractions 
  • Microsoft.Extensions.Configuration.Json

Create a class AppConfiguration in class library. Put the Database connection string in appsetting.json in main project.

"ConnectionStrings": {
"DataConnection": “Connection String”
},

Now we need to accessing the this connection string value from our AppConfiguration.cs in class library.

using Microsoft.Extensions.Configuration;
using System.IO;

namespace LogicLayers.AppConfig
{
    public class AppConfiguration
    {       
        public readonly string _connectionString = string.Empty;
        public AppConfiguration()
        {
            var configurationBuilder = new ConfigurationBuilder();
            var path = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
            configurationBuilder.AddJsonFile(path, false);

            var root = configurationBuilder.Build();
            _connectionString = root.GetSection("ConnectionString").GetSection("DataConnection").Value;
            var appSetting = root.GetSection("ApplicationSettings");
        }
        public string ConnectionString
        {
            get => _connectionString;
        }

    }
}

This how we obtain the connection string from the main project to class library.

Also read Blazor Framework

  • 1
  • 0
  • 2
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Anil Shrestha

Anil Shrestha

Qualified and skilled professional with experience working as a Software Developer

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey