How to read a key from the Web.Config file in ASP.NET app using C# ?

Published on February 10, 2016 by abundantcode

Filed under ASP.NET

Last modified February 9, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 3 (1 votes)

This article have been viewed 5093 times

If you want to read the values based on the key from the web.config file in your ASP.NET application using C# , you can use the  WebConfigurationManager to do it.

How to read a key from the Web.Config file in ASP.NET app using C# ?

Assume that the configuration file contains the below values

<configuration>
   <appSettings>
      <add key="UserName" value="username"/>
   </appSettings>
</configuration>

If you want to read the value of the key ("UserName") , you can use the WebConfigurationManager as shown below.

string userNameValue = WebConfigurationManager.AppSettings["UserName"]

Leave a Comment


x

You might be Interested in these Posts

How to read connection string from web.config in ASP.NET and C# ?

Here's a sample code snippet demonstrating how to get the connection string from your ASP.NET web.co...

How to return Json instead of XML in ASP.NET Web API ?

When you make a call to the ASP.NET Web API action method and notice this in Google Chrome browser ,...

How to Add Web API to existing ASP.NET Web Forms Project ?

In one of the previous posts , we saw how to add the WebAPI to the existing ASP.NET MVC project. Wel...