-1

I have created a appsettings.json file to store all constant information.

I am creating a .Net Core 2 console application, and need to access this through a global constant file.

What ever online resources I am seeing basically says how to access it in program.cs file only.

A Developer
  • 1,001
  • 3
  • 12
  • 32
  • 1
    There's no such thing as a "constant class". In any case, you cannot use `const` since that's at build-time and you want run-time values. If you refer to a `static` class with `static` variables, it's the same as any class so your question isn't clear – Camilo Terevinto Feb 12 '18 at 16:21
  • @CamiloTerevinto , basically I want to declare it once, and want to use it everywhere in the codebase, with out much repetitive code. – A Developer Feb 12 '18 at 16:59

1 Answers1

0

The appsettings.json Configuration can be inserted at any class using Dependency Injection

public class Test 
{
    public Test(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    private readonly IConfiguration Configuration; 
Mohamad Elnaqeeb
  • 541
  • 4
  • 13
  • 1
    If you have already answered the question, you need to flag this as a duplicate of that question, not post another answer which is a bad copy-paste. Please delete this – Camilo Terevinto Feb 12 '18 at 17:19
  • How would you call this class from another class? e.g. Test x = new Test(); (it wants me to put in the value for the IConfiguration configuration but I don't have it as it is injected via DI? – Rodney Mar 06 '18 at 06:15
  • by DI see https://stackoverflow.com/questions/47420522/read-appsettings-json-from-a-class-in-net-core-2/48728973#48728973 – Mohamad Elnaqeeb Mar 06 '18 at 19:57