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"; mySetting.Save();
Tagged: ASP.NET MVC