By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,293 Members | 1,579 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,293 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Nov 19 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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
"bradtm" <br****@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
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?

Nov 19 '05 #2

P: n/a
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")
--------------------------
Jean-Claude Morin, MCP
Software Developer
2k1Soft/kCentric, Canada
"bradtm" <br****@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
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?

Nov 19 '05 #3

P: n/a
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.

"Tampa.NET Koder" wrote:
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
"bradtm" <br****@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
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?


Nov 19 '05 #4

P: n/a
It won't let me use Server.MapPath in a Class Library.

"Jc Morin" wrote:
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")
--------------------------
Jean-Claude Morin, MCP
Software Developer
2k1Soft/kCentric, Canada
"bradtm" <br****@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
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?


Nov 19 '05 #5

P: n/a
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.BinD irectory
& "/../web.config")
--------------------------
Jean-Claude Morin, MCP
Software Developer
2k1Soft/kCentric, Canada
"bradtm" <br****@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.com...
It won't let me use Server.MapPath in a Class Library.

"Jc Morin" wrote:
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")
--------------------------
Jean-Claude Morin, MCP
Software Developer
2k1Soft/kCentric, Canada
"bradtm" <br****@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
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?


Nov 19 '05 #6

P: n/a
Cancel that.. I imported Server.Web.HttpServerUtility and called it by
MapPath(""). Gonna test it out now.

"bradtm" wrote:
It won't let me use Server.MapPath in a Class Library.

"Jc Morin" wrote:
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")
--------------------------
Jean-Claude Morin, MCP
Software Developer
2k1Soft/kCentric, Canada
"bradtm" <br****@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
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?


Nov 19 '05 #7

P: n/a
Awesome. That worked out. Thanks a lot.

"Jc Morin" wrote:
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.BinD irectory
& "/../web.config")
--------------------------
Jean-Claude Morin, MCP
Software Developer
2k1Soft/kCentric, Canada
"bradtm" <br****@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.com...
It won't let me use Server.MapPath in a Class Library.

"Jc Morin" wrote:
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")
--------------------------
Jean-Claude Morin, MCP
Software Developer
2k1Soft/kCentric, Canada
"bradtm" <br****@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
> 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?
>


Nov 19 '05 #8

P: n/a
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.

"bradtm" <br****@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
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?

Nov 19 '05 #9

P: n/a
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Thu, 24 Feb 2005 19:35:02 -0800, bradtm
<br****@discussions.microsoft.com> wrote:
Awesome. That worked out. Thanks a lot.

"Jc Morin" wrote:
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.BinD irectory
& "/../web.config")
--------------------------
Jean-Claude Morin, MCP
Software Developer
2k1Soft/kCentric, Canada
"bradtm" <br****@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.com...
> It won't let me use Server.MapPath in a Class Library.
>
> "Jc Morin" wrote:
>
> > 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")
> >
> >
> > --------------------------
> > Jean-Claude Morin, MCP
> > Software Developer
> > 2k1Soft/kCentric, Canada
> >
> >
> > "bradtm" <br****@discussions.microsoft.com> wrote in message
> > news:79**********************************@microsof t.com...
> > > 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?
> > >
> >
> >
> >



Nov 19 '05 #10

P: n/a
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).

--
Matt Berther
http://www.mattberther.com
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
"bradtm" <br****@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
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?


Nov 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.