Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC5 reading Config values without using System.Web

I keep reading that the new MVC avoids using System.Web by default as that used to add lots of extra stuff automatically.

Does that mean that in order to read the config file now in MVC5, one should use from System.Net instead?

Thanks...

like image 482
SF Developer Avatar asked Jul 07 '14 00:07

SF Developer


People also ask

Does .NET core need Web config?

In order to set up the ASP.NET Core Module correctly, the web. config file must be present at the content root path (typically the app base path) of the deployed app.

Does .NET core still use Web config?

Setup configurationASP.NET Core no longer uses the Global. asax and web. config files that previous versions of ASP.NET utilized.

Which section in a Web config file can be used to specify application level variables as key value pairs?

The key/value pairs specified in the <appSettings> element are accessed in code using the ConfigurationSettings class. You can use the file attribute in the <appSettings> element of the Web. config and application configuration files.

How do I open Web config file in Visual Studio?

Click Add to create the file and open it for editing. The file contains the code shown in the "Example" section later in this topic, with a few initial defaults. Your application inherits all configuration settings from the Machine. config and Web.


1 Answers

It should be:

string key = System.Configuration.ConfigurationManager.AppSettings.Get("AppKey");

OR

string key = System.Configuration.ConfigurationManager.AppSettings["AppKey"];
like image 83
Hiren Kagrana Avatar answered Nov 09 '22 12:11

Hiren Kagrana