Specifying configuration settings in Web. config

Source: Internet
Author: User
Tags configuration settings classic asp

Ref: http://aspnet.4guysfromrolla.com/demos/printPage.aspx? Path =/articles/053102-1.aspx

Introduction
There are often times when you need to store some globally accesile Ile bit of information that will be used on (nearly) every page on the web site. ideally this bit of information wocould be stored once in some centralized repository rather than being replicated on each and every web page. for example, a database connection string is one such bit of information. if such information isNotStored in a centralized location, instead typed in by hand on every page that needs to connect to the database, imagine what a headache it wocould be if the connection string changed-You wowould have to go through each data-driven page on the site and make the needed changes!

in classic ASP such global information was typically stored as an application variable. (keep in mind it was never smart to store an open connection object in application scope, but storing the connection string is fine .) in ASP. net there is still the notion of Application variables, which can be accessed in the same fashion as they were in classic ASP. however, there's also the capability to specify application-wide settings in the Web. config file. in this article we'll examine how to specify application-wide settings in Web. config , and how to read such settings from an ASP. net web page. we'll also look at how to specify custom settings in Web. config .

what is Web. config
in classic ASP all web site related information was stored in the metadata of IIS. this had the disadvantage that remote web developers couldn't easily make web-site configuration changes. if you host a web site with a company you're probably aware of this; for example, if you want to add a custom 404 error page, A setting needs to be made through the IIS Admin tool, and you're web host will likely charge you a flat tables to do this for you. (For more information on creating custom 404 error pages in classic ASP, see: creating a custom 404 error page .)

with ASP. net, however, these settings are moved into an XML-formatted text file ( Web. config ) that resides in the web site's root directory. through Web. config You can specify settings like custom 404 error pages, authentication and authorization settings for the web site, compilation options for the ASP. net web pages, if tracing shoshould be enabled, etc. for more on ASP. net configuration, be sure to read: Asp. net configuration.

Specifying application-wide settings inWeb. config
TheWeb. configFile, As aforementioned, is an XML-formatted file. at the root level is<Configuration>Tag. inside this tag you can add a number of other tags, the most common and useful one beingSystem. WebTag, where you will specify most of the Web site configuration parameters. However, to specify application-wide settings you use<Deleetask>Tag. inside of this tag you can specify zero to your settings by using<Add.../>Tag. For example, if we wanted to add a database connection string parameter we cocould haveWeb. configFile like so:

<Configuration> <! -- Application specific settings --><Deleetask> <add key = "connstring" value ="Connection string"/> </Appsettings><System. Web>... </system. Web> </configuration>

The above Code adds an application-wide setting namedConnstringWith the connection string value providedConnection string. Now, in any of your ASP. NET web pages in this web site you can read the value ofConnstringParameter like so:

Configurationsettings. deleettings ("connstring ")

In fact, if you examine any one of the DataGrid demos from the previous article a thorough examination of the DataGrid, you'll see that they all use this technique when needing to make a connection to the database. specifically, the code looks like:

Dim myconnection as new sqlconnection (Configurationsettings. deleettings ("connstring "))

Specifying classes of application-wide settings
If you are creating a large ASP. NET application you may (wisely) Decide to specify a number of web master-adjustable properties as application-wide parameters. While you can use Appsettings Tag as we have done in the article thus far, if you intend to publish this web application or have it be used on other people's web sites, placing such parameters in Appsettings Tag may lead to problems. For example, imagine that you have a connection string parameter named Connstring That you want to let be allowed, so you create a key in Appsettings Tag named Connstring , As we did above. well, imagine that the person who's installing your application is trying to integrate with his already-existing web site. she might already have such configuration settings, which means she'll have to change her setting and all the pages that reference it, to some name That doesn' t conflict with any of your setting names.

Ideally you 'd like not to present your end user with this headache. To avoid this complication you can "group" your application's settings into a unique tag inWeb. configFile. That is, you can create a tag named:<Myappsettings>InWeb. configAnd then use the <...> as we did earlier to add application-wide settings. To add a custom tagWeb. configYou need to first explicitly specify the new tag name inWeb. configVia<Configsections>Tag, like so:

<Configuration><Configsections> <section name = "myappsetction" type =" system. configuration. namevaluefilesectionhandler, system, version = 1.0.3300.0, culture = neutral, publickeytoken = regular "/> </configsections>... </Configuration>

Note that the value ofTypeAttribute of<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 namedMyappsettings. Now we can add<Myappsettings>Tag to ourWeb. configFile and add<Add.../>Tags to add application-wide parameters, like so:

<Configuration> <configsections> <section name = "myappsettings" type = "system. configuration. namevaluefilesectionhandler, 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 ")

More generally, replaceMyappsettingsWith whatever name you chose to give your custom settings tag, and replaceConnstringWith whatever name of the parameter in the custom settings tag you wish to read. this way, when shipping your asp. net web application, the person installing it won't run into any naming conflicts (unless, of course, they also have created a custom settings tag with the exact same name as you had chosen ).

Creating richer custom configuration sections
Using a "group" of application settings works well to disambiguate your custom configuration settings from other application settings, but is limiting in the way that it only allows for scalar name/value pairs for configuration information. but what if your configuration information needs a collection of settings, where there may be an arbitrary number of values in the collection?

To enjoy a more readable and richer custom configuration experience you can create custom configuration sections inWeb. config, Resulting in configuration settings like the following:

<Scottssettings
Message = "Hello, world! "
Showmessageinbold = "true">
<Favoritecolors>
<Color> Red </color>
<Color> yellow </color>
<Color> brown </color>
</Favoritecolors>
</Scottssettings>

To accomplish this you need to create a couple of classes to model the configuration information and to pump the custom configuration markup to a method that deserializes it. for more information on this topic, see: creating custom configuration sections inWeb. config.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.