In ASP.NET application, we can store dynamic data in the configuration file, specifically in Web.config
. This helps avoid hardcoding data within the program. After the application is deployed, maintainers can modify the configuration file to change parameter values without altering the source code.
Find the appSettings
node in Web.config
, The following are the default settings
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
Since appSettings
is key-value type, the setup is straightforward, Assign a custom-defined name to the key which can be considered the parameter's name, and assign the data value to be stored to the value.
<appSettings>
<add key="Greeting" value="Hello World" />
</appSettings>
And next, read appSettings
from the controller, First, using the System.Configuration
namespace. The reading method is also very simple.
using System.Configuration;
public ActionResult GetAppSettingsProperty()
{
var message = ConfigurationManager.AppSettings["Greeting"].ToString();
return Content(message);
}
Job done☑️
Thanks for reading the article
If you like it, please don't hesitate to click heart button ❤️
or follow my GitHub I'd appreciate it.
Top comments (0)