Working with the configuration and perform various settings.

Configuration files are used to set the common values that we will require in all the pages. In this article we will see different type of setting that we can set in the configuration file and then how can we use it in the pages.

In the web applications we use web.config file and in windows application we use app.config file.
Here we will discuss about the web.config file.

Note: Must add the The System.Web.Configuration namespace.

Following are some of the things that we set in this file and then use it through out the application.

1. we can add the custom configuration setting inside the appSettings section.
Below we are declaring a welcome message in the web.config file and then use it in other page:

<add key="welcome" value="Welcome To Online Shop Portal!" />

and then we can access it through page like:

lblmessage.Text = WebConfigurationManager.AppSettings["welcome"];

2. We can set the connectionstring of the database that we are using:

<add name="OnlineConnString" connectionString="Data Source=dell;Initial Catalog=OnlineShop;
User ID=sa;Password=sa;Persist Security Info=False;" providerName="System.Data.SqlClient"/>

and then use it in our page like:

string c = ConfigurationManager.ConnectionStrings["OnlineConnString"].ConnectionString;

3. If we are using master page and want to apply a common design in all the pages then we can
register the master page in the config file like:

<pages masterPageFile="~/Home.master" />

4. Similarly we can register the themes in the config file like:

<pages theme="DefaultTheme" />


5. We can register the usercontrol in the config file like:

<add tagPrefix="welcome" tagName="WelcomeImage" src="~/UserControls/WelcomeImage.ascx"/>

and then can use it in page like:

<welcome:WelcomeImage ID="WelImageId" Runat="Server" />

6. We can specify the session timeout time of the application in the config file like:

<sessionState timeout="60" />

7. We can specify the culture and uiculture in the config file like:

<globalization culture="de-DE" uiCulture="de-DE" />

8. We can specify the page on which to redirect if any error occurs.

<customErrors mode="RemoteOnly" defaultRedirect="~/Logout.html">
<error statusCode="403" redirect="NoAccess.htm"/>
<error statusCode="404" redirect="FileNotFound.htm" />

These are some of the common things that we set in the configuration file.


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: