How to read web.config file in .Net Core app

后端 未结 6 1495
逝去的感伤
逝去的感伤 2020-12-01 15:51

I have created a .Net Core API and I referenced a .Net framework application to it. The referenced Application connects to a data base and its connection string is stored in

6条回答
  •  情书的邮戳
    2020-12-01 16:55

    In case you missed it - and because @Zuhair doesn't seem to want to post an answer - here is a copy paste of their solution (I missed this at first as it was only in a comment):

    I found the solution. I changed the name of the Web.config to app.config and I was able to get the connection string using:

    System.Configuration.ConfigurationManager.ConnectionStrings["SHOPPINGCNN"].ConnectionString

    The app.config file looks like this:

     encoding="utf-8" ?> 
     
     
     
     
    
    

    You also need to install this NuGet package:

    System.Configuration.ConfigurationManager
    

    In my case I simply renamed the web.config containing the connectionString 'app.config' and it worked.

    I realise this probably isn't a good long term solution but for mixed legacy projects - or to get a foot in the door to begin to learn .net Core it's very useful.

提交回复
热议问题