0

I am trying to access the SQL connection string within .net core api project. My controller is using the following code to get it but the value of szDSN remains null.

string szDSN = Environment.GetEnvironmentVariable("DSNSQLConnectionString");

and I add it in my appsettings.json like this

"Values": {
    "DSNSQLConnectionString": "Data Source=(local);Initial Catalog=local;User Id=abc; Password=123"
  },

I used the same way of getting my SQL connection in .net core project of type functions and it worked fine.. But for some reason its not pulling the value from appsettting.json in API type of project. What am I doing incorrectly?

I am going to need to add many more configuration variables in this project which I will need in the controllers, not sure why Environment.GetEnvironmentVariable is not grabbing variable values from appsettings.json

Sarah
  • 1,199
  • 2
  • 21
  • 42
  • 1
    Please let me know if the duplicate doesn't answer your question by tagging me in a comment (@john) – ProgrammingLlama Jul 26 '19 at 02:17
  • 1
    @John Thanks much for helping with the duplicate post link. I looked it up but it took me it didnt have clear answer for getting configuration values in a controller. However this link helped n case someone else has the same question. https://dzone.com/articles/read-configuration-in-the-controller-net-core-quic – Sarah Jul 29 '19 at 23:18

1 Answers1

1

As far as I know, Environment.GetEnvironmentVariable is for Windows Variables, not your projects.

John
  • 5,942
  • 3
  • 42
  • 79