Share via

How do I access secrets.json from any class?

Trott, Dwayne 20 Reputation points
Jun 28, 2023, 2:04 AM

I have a .NET Core API project.

I can access the values in Program.cs, but I want to be able to read from other classes e.g. Respository class.

.NET 6.0

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,645 questions
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,546 Reputation points Microsoft Vendor
    Jun 28, 2023, 7:31 AM

    Hi @Trott, Dwayne

    How do I access secrets.json from any class? I have a .NET Core API project. I can access the values in Program.cs, but I want to be able to read from other classes e.g. Respository class.

    You can use JSON configuration provider to load the secrets.json file, then access the value via IConfiguration.

    Refer to the following sample:

    1.Add a secrets.json file:

    User's image

    2.Load the secrets.json file in the program.cs file:

    
    //load the json file.
    builder.Configuration.AddJsonFile("secrets.json",
            optional: true,
            reloadOnChange: true);
    //register the service 
    builder.Services.AddScoped<IDataRepository, DataRepository>();
    
    //get the value from the secrets.json file
    var name = builder.Configuration.GetSection("Employee:name").Value;
    

    3.The DataRepository service like this:

        public interface IDataRepository
        {
            string GetEmployeeName();
        }
        public class DataRepository : IDataRepository
        {
            public IConfiguration _config;
            public DataRepository(IConfiguration configuration)
            {
                _config=configuration;
            }
             
            public string GetEmployeeName()
            {
                return _config.GetSection("Employee:name").Value;
            }
        }
    

    After that, in the API controller, we can inject the service and then get the value.

        [Route("api/[controller]")]
        [ApiController]
        public class EmployeeController : ControllerBase
        {
            private readonly IDataRepository _datarepo;
    
            public EmployeeController(IDataRepository dataRepository)
            {
                _datarepo=dataRepository;
            }
            public string Get()
            {
                return _datarepo.GetEmployeeName();
            }
        }
    

    More detail information, see Configuration in ASP.NET Core.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.