DEV Community

FakeStandard
FakeStandard

Posted on • Edited on

[ASP.NET] Setting and Retrieving Custom Data from Web.config

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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.


Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay