MapPath equivalent from static method

M

Martin Eyles

Hi,
I have an xml config file for my website, which stores all the
information required to connect to a database. I have used the following
code to get the xml file into my program as an object, and this works
beautifully, as long as I directly call it from my codebehind.

Dim MyConfigXml As New System.Xml.XmlDataDocument
MyConfigXml.Load(aHttpServUtil.MapPath("My.config"))

However, as the settings are common I would like to use this in a static
method (sorry, just realised, I mean shared function - I learnt programing
in java mainly, so I sometimes mix up the terms) of an object, and call it
from all my pages. Unfortunately, when I do
this the MapPath gives compiler errors, and I have to manually enter the
entire path into the method. Are there any other ways of getting the path -
the path of the config file is the same as that of all of my .vb files. I am
using the VB.net language, .net framework 1.1 and VS.net 2003.

Thanks,
Martin
 
P

Patrice

You forgot to mention what is the exact compilation error you have. A common
error in this context could be to try to access a non shared member inside a
shared member.

You may want also to have a look at the documentation for the "web.config"
file. This is the usual configuration file for web applications and it could
provide perhaps what you are trying to do...
 
M

Martin Eyles

Patrice said:
You forgot to mention what is the exact compilation error you have. A
common error in this context could be to try to access a non shared member
inside a shared member.

Ok, here is what you need to know I think. If I used the class directly, I
get "Reference to a non-shared member requires an object reference.". If I
try to make an object of this class, I get an error when trying to
instantiate it (with the code "Dim aHttpServUtil As New
HttpServerUtility") - "Overload resolution failed because no 'New' is
accessible.".
You may want also to have a look at the documentation for the "web.config"
file. This is the usual configuration file for web applications and it
could provide perhaps what you are trying to do...

I want my own config file, with just my own configuration data - this makes
it easy to install on different server setups with different database
servers. It also makes it harder for us to break the system by changing the
wrong thing in web.config. Thanks for the idea though - it is appreciated

Thanks,
Martin
 
G

Günter Prossliner

Hi Martin!
If I try to make an object of this class, I get an error
when trying to instantiate it (with the code "Dim aHttpServUtil As New
HttpServerUtility") - "Overload resolution failed because no 'New' is
accessible.".

You just have to use the current HttpContext:

string s = HttpContext.Current.Server.MapPath("file.xml");


This only works, if there is a current HttpContext. So you cannot use this
e.g. in static constructors, or in background Threads you have created
(HttpContext.Current is null under this circumstances). But if you use this
in a static functions, witch is called somewhere in the middle of a Http -
Request, it is no problem.

OK?
br, GP
 
M

Martin Eyles

Günter Prossliner said:
Hi Martin!


You just have to use the current HttpContext:

string s = HttpContext.Current.Server.MapPath("file.xml");


This only works, if there is a current HttpContext. So you cannot use this
e.g. in static constructors, or in background Threads you have created
(HttpContext.Current is null under this circumstances). But if you use
this in a static functions, witch is called somewhere in the middle of a
Http - Request, it is no problem.

OK?
br, GP

Thanks very much - it works a treat. :)

Martin
 

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