ARTICLE

Using Web.Config in ASP.NET and ASP

Posted by Sushila Patel Articles | ASP.NET Programming January 09, 2004
Web.config acts as a central location for storing the information to be accessed by web pages.
Reader Level:

Introduction: 

Web.config acts as a central location for storing the information to be accessed by web pages. This information could be a Connection String stored at a centralized location so that it can be accessed in a data-driven page. If the connection string changes its just a matter of changing it at one place.

In classic ASP such global information was typically stored as an application variable.

In the sample we'll read the information from web.config using ASP.NET and ASP as there could be a possibility of project having ASP and ASP.NET pages.

web.config:

Contains a key-value pair.
web.config

<?xml version="1.0" encoding="utf-8"?>
<
configuration>
<
appSettings>
<add key="ConnectionString1" value="server=localhost;uid=sa;pwd=;database=northwind" />
<add key="ConnectionString2" value="server=localhost;uid=sa;pwd=;database=pubs" />
</
appSettings>
</
configuration>

In ASP.NET: 

We can just using Configuration.AppSettings(<key>) gives the Value.(Namespace:System.Configuration)

VB.NET

Dim strConnection As String
strConnection = ConfigurationSettings.AppSettings("ConnectionString1")
Response.Write(strConnection)

C#

string strConnection;
strConnection = ConfigurationSettings.AppSettings["ConnectionString1"];
Response.Write(strConnection);

In ASP

We need to iterate through the nodes in web.config.

vbscript

set xmlDoc=server.CreateObject("Microsoft.XMLDOM")
set xmlappSettings=server.CreateObject("Microsoft.XMLDOM")
set xmladd=server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load(server.MapPath ("web.config"))
set xmlappSettings = xmldoc.GetElementsByTagName("appSettings").Item(0)
set xmladd = xmlappSettings.GetElementsByTagName("add")
for each x in xmladd
'Check for the Atrribute Value
if  x.getAttribute("key") ="ConnectionString1" then
Response.write(x.getAttribute("value"))
end if
next

Login to add your contents and source code to this article
post comment
     

thnk u for ur best information...

Posted by krishna angirekula Apr 02, 2013

<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <!-- DYNAMIC DEBUG COMPILATION Set compilation debug="true" to enable ASPX debugging. Otherwise, setting this value to false will improve runtime performance of this application. Set compilation debug="true" to insert debugging symbols (.pdb information) into the compiled page. Because this creates a larger file that executes more slowly, you should set this value to true only when debugging and to false at all other times. For more information, refer to the documentation about debugging ASP.NET files. --> <compilation defaultLanguage="c#" debug="true" /> <!-- CUSTOM ERROR MESSAGES Set customErrors mode="On" or "RemoteOnly" to enable custom error messages, "Off" to disable. Add <error> tags for each of the errors you want to handle. "On" Always display custom (friendly) messages. "Off" Always display detailed ASP.NET error information. "RemoteOnly" Display custom (friendly) messages only to users not running on the local Web server. This setting is recommended for security purposes, so that you do not display application detail information to remote clients. --> <customErrors mode="RemoteOnly" /> <!-- AUTHENTICATION This section sets the authentication policies of the application. Possible modes are "Windows", "Forms", "Passport" and "None" "None" No authentication is performed. "Windows" IIS performs authentication (Basic, Digest, or Integrated Windows) according to its settings for the application. Anonymous access must be disabled in IIS. "Forms" You provide a custom form (Web page) for users to enter their credentials, and then you authenticate them in your application. A user credential token is stored in a cookie. "Passport" Authentication is performed via a centralized authentication service provided by Microsoft that offers a single logon and core profile services for member sites. --> <authentication mode="Windows" /> <!-- AUTHORIZATION This section sets the authorization policies of the application. You can allow or deny access to application resources by user or role. Wildcards: "*" mean everyone, "?" means anonymous (unauthenticated) users. --> <authorization> <allow users="*" /> <!-- Allow all users --> <!-- <allow users="[comma separated list of users]" roles="[comma separated list of roles]"/> <deny users="[comma separated list of users]" roles="[comma separated list of roles]"/> --> </authorization> <!-- APPLICATION-LEVEL TRACE LOGGING Application-level tracing enables trace log output for every page within an application. Set trace enabled="true" to enable application trace logging. If pageOutput="true", the trace information will be displayed at the bottom of each page. Otherwise, you can view the application trace log by browsing the "trace.axd" page from your web application root. --> <trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" /> <!-- SESSION STATE SETTINGS By default ASP.NET uses cookies to identify which requests belong to a particular session. If cookies are not available, a session can be tracked by adding a session identifier to the URL. To disable cookies, set sessionState cookieless="true". --> <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20" /> <!-- GLOBALIZATION This section sets the globalization settings of the application. --> <globalization requestEncoding="utf-8" responseEncoding="utf-8" /> </system.web> </configuration>

Posted by Sutha Raghavan Jul 21, 2011

Hi Sir,

   How to sent SMS from PC to Mobile........If anyone know that Pls sent some sample code ( ASP.NET)..........
My EmailID:  jeganitrvs@gmail.com
      

Posted by Jegan Mohan Nov 22, 2010

Can one solution can have multiple web.config file?If yes how can we use multiple web.config files

Posted by ankit sharma Aug 26, 2010

Most of the time developers register the User Controls to individual asp.net pages where, controls are in use. If the control is in use in most of the pages of the site, the reference can be added at one place only, that is in the web.config.
 
 
Code to add in web.config file to register the user control
<configuration>
  <system.web>
    <pages>
      <controls>
        <add tagPrefix="uc1" src="~/usercontrols/Note.ascx" tagName="Note"/>
        <add tagPrefix="uc2" src="~/usercontrols/Diary.ascx" tagName="Diary"/>
      </controls>
    </pages>
  </system.web>
</configuration>

Posted by eliza sahoo Apr 29, 2010
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts