IT Community - Software Programming, Web Development and Technical Support

Web.config

This is a discussion on Web.config within the ASP and ASP.NET Programming forums, part of the Web Development category; What is Web.config? What is its' relation to ASP.net? What are the advantages of using such configuration? Or ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 04-09-2007, 08:22 AM
nhoj nhoj is offline
D-Web Programmer
 
Join Date: Apr 2007
Posts: 95
nhoj is on a distinguished road
Default Web.config

What is Web.config? What is its' relation to ASP.net? What are the advantages of using such configuration? Or is there a better configuration you can use to make you work more efficient?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 04-24-2007, 07:26 PM
ewriter ewriter is offline
D-Web Trainee
 
Join Date: Apr 2007
Posts: 17
ewriter is on a distinguished road
Default Re: Web.config

I am using webconfig to make global connection so that I dont have to repeat connection on each page.

HTML Code:
<add key="conn" value="server=itcodes1;database=Scriptlance;uid=sa;password="/>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 11-21-2007, 09:01 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: Web.config

u can disable HTTP/POST and HTTP/GET Protocols
by the following code
Unless you specify otherwise, .NET will attempt to bind your Web services to three separate protocols: HTTP/POST, HTTP/GET, and SOAP. We say attempt, because depending on the parameter and return types for a service, the HTTP/GET protocol may not be possible. Bindings for these three protocols will be included in the WSDL file automatically generated by .NET, and consumer clients will have the option of choosing any one of them for communication with your service.

You can easily remove these bindings by adding the following section to your Web.config file:


<webservices>
<protocols>
<remove name="HttpPost" />
<remove name="HttpGet" />
</protocols>
</webservice>
his section will tell the WSDL generator not to include bindings for HTTP/POST and HTTP/GET. The two remove sections specify that HttpPost and HttpGet should not be supported.

Security and interoperability are two good reasons to avoid exposing your Web services using HTTP/POST or HTTP/GET. HTTP/GET is less secure than SOAP, and since HTTP/GET is commonly used for Web linking, a malicious user could potentially trick someone into unknowingly calling a Web service using their security credentials when they thought they were clicking a Web link.

With regard to interoperability, where SOAP is a widely-used standard for Web service communication, HTTP/GET and HTTP/POST are not. As a result, many automatic proxy generation tools weren't designed to "understand" the HTTP/GET and HTTP/POST bindings included by default in a .NET-generated WSDL document. If your service doesn't make use of these bindings, removing them can increase your service's interoperability.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 11-22-2007, 04:16 AM
KiruthikaSambandam KiruthikaSambandam is offline
D-Web Analyst
 
Join Date: Aug 2007
Posts: 328
KiruthikaSambandam is on a distinguished road
Default Re: Web.config

Web.config

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=north wind" />
<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("ConnectionStrin g1")
Response.Write(strConnection)

C#

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

ThankQ
KiruthikaSambandam
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 11-22-2007, 04:53 AM
sathian sathian is offline
D-Web Analyst
 
Join Date: Aug 2007
Posts: 246
sathian is on a distinguished road
Default Re: Web.config

Web.config

ASP.NET provides a configuration system we can use to keep our applications flexible at runtime. In this article we will examine some tips and best practices for using the configuration system for the best results.

The <appSettings> element of a web.config file is a place to store connection strings, server names, file paths, and other miscellaneous settings needed by an application to perform work. The items inside appSettings are items that need to be configurable depending upon the environment, for instance, any database connection strings will change as you move your application from a testing and staging server into production.

As an example, let’s examine this minimal web.config with an appSetting entry to hold our connection string:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.web>

<compilation defaultLanguage="c#" debug="true" />

</system.web>

<appSettings>

<add key="ConnectionInfo" value="server=(local);database=Northwind;Integrate d Security=SSPI" />

</appSettings>

</configuration>

To read the key we use the ConfigurationSettings class from the System.Configuration namespace, as shown below.


private void Page_Load(object sender, EventArgs e)
{

string connectionInfo = ConfigurationSettings.AppSettings["ConnectionInfo"];
using(SqlConnection connection = new SqlConnection(connectionInfo))
{

connection.Open();
// perform work with connection
}

}

Some applications will duplicate these lines of code in many places. If we think about how the above code will evolve over time, we might see a handful of weaknesses. First, we have a hard coded string in place to fetch connection information from the web.config. Hard coded strings can be easy to mistype and difficult to track down if the key ever changes. Secondly, the code will tie us forever to the appSettings section of the web.config file. Although web.config is designed for just such a setting, we might find in the future that we need to pull configuration settings from a database, or change a setting from being application-wide to being a per-user configuration item kept in the Session or in a cookie.

Encapsulation

Let’s abstract away the source of the connection string using a class with a static property.

using System.Configuration;

namespace aspnet.config

{

public class Configuration

{

public static string ConnectionInfo

{

get

{

return ConfigurationSettings.AppSettings["ConnectionInfo"];

}


}

}

}

Now in Page_Load method looks like the following.

private void Page_Load(object sender, EventArgs e)

{

using(SqlConnection connection = new SqlConnection(Configuration.ConnectionInfo))

{

connection.Open();

// perform work with connection

}

}

The changes we made to the above code were relatively small - but powerful. Now the Page_Load function doesn’t know where the connection string comes from. We could easily change the ConnectionInfo property to retrieve the string from a different source without modifying any other code in the application. We also no longer have to remember the key string and hard code the string at various points in the application. Instead, we can utilize Visual Studio Intellisense when accessing the Configuration class to find the setting we need. The key value only appears once – inside the ConnectionInfo property – so we could again change the key name in the web.config and have only one line of code to update.

Of course this approach only works if all of the code in the application goes to the Configuration class instead of directly to web.config to retrieve settings. For each appSetting entry added to web.config we would add a corresponding property to the Configuration class.

Another advantage to this approach becomes apparent if we have an application setting that is not a string, but a date, integer, or other primitive type. In this case we could have the corresponding Configuration property parse the string from the web.config file and return the appropriate type.

You’ll notice we provided a read-only property for ConnectionInfo. The web.config file is not designed for constant updates. When an ASP.NET application launches a watch is put on the web.config file. ASP.NET will detect if the web.config changes while the application is running. When ASP.NET detects a change it will spin up a new version of the application with the new settings in effect. Any in process information, such as data kept in Session, Application, and Cache will be lost (assuming session state is InProc and not using a state server or database).

Next, let’s take a look at a little known feature of appSettings that can give us even more flexibility.

Multiple File Configuration

The appSettings element may contain a file attribute that points to an external file. Let’s change our web.config to look like the following.

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.web>

<compilation defaultLanguage="c#" debug="true" />

</system.web>

<appSettings file="testlabsettings.config"/>

</configuration>

Next, we can create the external file ‘testlabsettings.config’ and add an appSettings section with our connection information.

<appSettings>

<add key="ConnectionInfo" value="server=(local);database=Northwind;Integrate d Security=SSPI" />

</appSettings>

if the external file is present, ASP.NET will combine the appSettings values from web.config with those in the external file. If a key/value pair is present in both files, ASP.NET will use the value from the external file.

The feature demonstrated above is useful when you keep user-specific or environment-specific settings in the external file. Let web.config contain those settings that are global to all the installed instances of your application, while each user or installed site contains their own settings in an external file. This approach makes it easier to move around global web.config changes and keep web.config checked into source control, while each developer can get their own settings separate.

I hope this answer has given you some tips on using the Web.config.

Thanks
Sathian.K
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 12-10-2007, 08:41 PM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 881
kingmaker is on a distinguished road
Send a message via MSN to kingmaker
Default Re: Web.config

Appsetting is older fashion....Current versions are supporting the separate ConnectionString element to maintain the connection string in Web.config

<connectionStrings>
<add name="Conn" connectionString="Data Source=server;Initial Catalog=DB;uid=uid;pwd=password;"
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 12-10-2007, 08:48 PM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 881
kingmaker is on a distinguished road
Send a message via MSN to kingmaker
Default Re: Web.config

the above connectin string is accessed by

ConfigurationManager.ConnectionStrings["Conn"].Name
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old 12-10-2007, 10:03 PM
sathian sathian is offline
D-Web Analyst
 
Join Date: Aug 2007
Posts: 246
sathian is on a distinguished road
Default Re: Web.config

Use the appSettings part of the Web.Config to store connection strings. This xml can then be modified anytime later and the next time a user browses to the site it will pick this new setting up.

One connection string is to Oracle and the Other is to SQL server

<appSettings>
<add key="ORACLEConnectionString" value="Provider=OraOLEDB.Oracle.1;
Persist Security Info=False;Password=blah;User ID=greg;Data Source=sph;" />
<add key="SQLConnectionString" value="data source=SQL1;initial catalog=ID_V;
integrated security=SSPI;persist security info=False;
packet size=4096"/>
<appSettings>


To get the values of the connections trings you would use

string conn = ConfigurationSettings.AppSettings["ORACLEConnectionString"];
OleDbConnection myConnection = new OleDbConnection(conn);


When the site goes live change the debug setting to false which will make the site have a little better performance.

<compilation defaultLanguage="C#" debug="true" />


Customer errors can be handled be turned off but I prefer them to be turned on as below.

<customErrors mode="Off" />


Or

<customErrors defaultRedirect="ErrorPage.aspx" mode="On">
<error statusCode="500" redirect="servererror.aspx" />
<error statusCode="404" redirect="filenotfound.aspx" />
<error statusCode="403" redirect="AccessDenied.aspx" />
</customErrors>


Application Tracing can be set up while shows the output times and all sorts of info of every page if you set it the values to true below. Local only would mean only onthe webserver hosting the site. You can also do per page tracing so that you can turn off application Tracing and have trace="true" at the top of a single page.

<trace enabled="true" requestLimit="10" pageOutput="true" traceMode="SortByTime"
localOnly="true"/>

Thanks
Sathian.K
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9  
Old 12-10-2007, 10:07 PM
sathian sathian is offline
D-Web Analyst
 
Join Date: Aug 2007
Posts: 246
sathian is on a distinguished road
Default Re: Web.config

Specifying Classes of Application-Wide Settings

<configuration>
<configSections>
<section name="MyAppSettings"
type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.3300.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
</configSections>

...
</configuration>

Note that the value of the type attribute of the <section ... /> tag must all appear on one line... the line breaks are in there for better formatting.

This <section ... /> tag indicates that we are going to be adding a custom tag named MyAppSettings. Now we can add a <MyAppSettings> tag to our Web.config file and add <add ... /> tags to add application-wide parameters, like so:

<configuration>
<configSections>
<section name="MyAppSettings"
type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.3300.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
</configSections>

<MyAppSettings>
<add key="connString" value="connection string" />
</MyAppSettings>


...
</configuration>

Finally, to read this custom value from an ASP.NET Web page we use the following syntax:

ConfigurationSettings.GetConfig("MyAppSettings")(" connString")

Thanks
Sathian.K
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Config File or Registry shaalini Server Management 4 06-14-2009 08:41 PM
Change the Asp.net (C#) Web.config file a.deeban ASP and ASP.NET Programming 1 08-18-2007 03:42 AM
What is Machine.config? H2o ASP and ASP.NET Programming 1 07-24-2007 02:18 AM
How to encrypt app.config file in .net.? bluesky C# Programming 0 07-17-2007 09:59 PM
How to Edit the web.config file elements using c#? Archer C# Programming 0 07-17-2007 09:55 PM


All times are GMT -7. The time now is 04:26 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0