Access Web.Config from a seperate Class Library in same Solution

G

Guest

So I have this hierarchy:

+ Solution
|
|--- + Class Library Project
| |
| |--- LogError.vb
|
|--- + ASP .NET Web Application
|
|--- Web.config

I want to access from Web.config:

<appSettings>
<add key="sqlConnString" value="myDBString">
</appSettings>

from LogError.vb. Anyone know how I would go about doing this?
 
T

Tampa.NET Koder

First of all, it is my understanding that this sort of thing is not allowed.

Second, I am curious on why you want to do this.

Lastly, the web.config is just a text file and you can treat it as such if
you just need to read the contents. But; don't expect to configure your
class library project using the web.config file. Create a new config file
Or, use the machine.config file
 
J

Jc Morin

Hi Brad.
If you want to read the binary web.config file you will need to use the
Server.MapPath methods.

Dim reader As New System.IO.StreamReader(Server.MapPath("web.config"))
Dim value As String = reader.ReadToEnd

If you want to read the value inside the web.config section appSettings just
use the ConfigurationSetting namespace

Dim sqlConStr As String =
ConfigurationSettings.AppSettings("sqlConnString")
 
G

Guest

I built an Application to log and view all the errors and bugs our
applications have. We use a common Class Library Project in many of our
applications. The LogError function is in one of these Class Libraries. In
order to store the Application ID of the error I need to access the
Application ID key in the Web.config file.
 
G

Guest

Cancel that.. I imported Server.Web.HttpServerUtility and called it by
MapPath(""). Gonna test it out now.
 
J

Jc Morin

Your right!

If you add a reference to System.Web
You can probably do a work around and calculate the path like this:

Dim reader As New System.IO.StreamReader(System.Web.HttpRuntime.BinDirectory
& "/../web.config")
 
G

Guest

Awesome. That worked out. Thanks a lot.

Jc Morin said:
Your right!

If you add a reference to System.Web
You can probably do a work around and calculate the path like this:

Dim reader As New System.IO.StreamReader(System.Web.HttpRuntime.BinDirectory
& "/../web.config")
 
P

Peter Rilling

My interpretation is slightly different then the other posts. I understand
your question as simply being able to access the configuration information
from the class library the same way you would in the web app (such as
ConfigurationSettings.AppSettings["..."]).

If this is your question, you can add a reference to your class library in
the web app. From there you can access any of the configuration settings
normally.
 
S

Scott Allen

As Peter mentioned in the other thread you should be able to use
ConfigurationSettings.AppSettings. This reads the settings for the
application your library is hosted in. No need to manually open and
parse the web.config.
 
M

Matt Berther

Hello Tampa.NET Koder" t_davisjr[at]hotmail.com,

Why cant you use the ConfigurationSettings api to access those settings?
As long as your ASP.NET app has a reference to the class library project,
the class library can use that API to retrieve settings from the parent applications
config file (either app.config or web.config).
 
Joined
Nov 15, 2006
Messages
1
Reaction score
0
The Answer

The answer i think you were after was to add this line of code in your class file:

Imports System.Configuration
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top