Load and Save ASP.NET MVC Settings

We have been using Settings both in WPF and WinForm.You might need to use Settings on your ASP.NET MVC application as well.Contrary to others, in MVC we should use web.config file to save and load our setting values.About security measures, if you are going to use web.config to save your password or other sensitive data,you need to encrypt them.

Firstly, we should add our value to the web.config file :

    <add key="returnEmployee" value="true">

to load our setting you should call OpenWebConfiguration method which is belongs WebConfigurationManager class.

 var mySetting = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~\\Views");
 var mySettingValue = mySetting.AppSettings.Settings["returnEmployee"].Value;

Keep in mind that “~\\Views” is a path of your web.config file.For saving your setting you just need to set some data to your setting and call Save() method as shown below.

  var mySetting = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~\\Views");
    mySetting.AppSettings.Settings["returnEmployee"].Value = "someValue";


Cavab yaz

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: