Forem

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.


Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay