web.config in a class

  • Thread starter Thread starter Jerry C
  • Start date Start date
J

Jerry C

I need to access the web.config file in a class. I am now using the line:

rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration("~")

to get a reference to the config file. The question is the correct way to
get the config file in classes. There is only one config file and it is in
the root of the web site. I do not want to use a path since the location of
the web site can change. The classes will be in the App_Code dir.



Thank you,
 
Your cide looks correct to me. You are using the tilde to indicate the root
directory, which is always relative to the site, so should work wherever you
put the code. What problem are you having with this?

Also, are you planning to read or write the web.config? Note that changes in
the web.config file will cause the Web server to restart, and will also
refresh the cache entries, which may cause problems in your application

HTH
 
Steve,

Thank you for you reply,
I am not having any problems with the code. I was checking the community to
see if I was using the correct code. This app started with framework 1 and I
run into old code. Just checking for the latest.

Thank you
 
Hi Jerry,

Yes your code is correct.

Regards,
Allen Chen
Microsoft Online Support
--------------------
| Thread-Topic: web.config in a class
| thread-index: Ackn+DHlonxjN67AShKQCxnenpaCCQ==
| From: =?Utf-8?B?SmVycnkgQw==?= <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: web.config in a class
| Date: Mon, 6 Oct 2008 14:12:09 -0700
| Lines: 46
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3119
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Path: TK2MSFTNGHUB02.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet:77402
| NNTP-Posting-Host: tk2msftibfm01.phx.gbl 10.40.244.149
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Steve,
|
| Thank you for you reply,
| I am not having any problems with the code. I was checking the community
to
| see if I was using the correct code. This app started with framework 1
and I
| run into old code. Just checking for the latest.
|
| Thank you
| --
| Jerry
|
|
| "Steve" wrote:
|
| > Your cide looks correct to me. You are using the tilde to indicate the
root
| > directory, which is always relative to the site, so should work
wherever you
| > put the code. What problem are you having with this?
| >
| > Also, are you planning to read or write the web.config? Note that
changes in
| > the web.config file will cause the Web server to restart, and will also
| > refresh the cache entries, which may cause problems in your application
| >
| > HTH
| >
| > | > >
| > > I need to access the web.config file in a class. I am now using the
line:
| > >
| > > rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration("~")
| > >
| > > to get a reference to the config file. The question is the correct
way to
| > > get the config file in classes. There is only one config file and it
is in
| > > the root of the web site. I do not want to use a path since the
location
| > > of
| > > the web site can change. The classes will be in the App_Code dir.
| > >
| > >
| > >
| > > Thank you,
| > >
| > > --
| > > Jerry
| >
| >
| >
|
 
In my class library projects I'm using the following code to access the
configuration file:

string myValue =
System.Configuration.ConfigurationManager.AppSettings.Get("myEntry");

This code exists since the .NET 1.x times and still works. It has (IMO) one
advantage over your solution, which is that it also works when the class
library is used by a windows forms application.
 
Back
Top