server.mappath in a vb class

M

MattB

If I use server.mappath to get to the root dir of my application from within
codebehind, it works fine. I also want to use this from a vb class I have in
my application, and I think I have an issue with context because
server.mappath does not work there.
I was thinking I'd just need to be more specific with the namespace and
tried HttpServerUtility.MapPath() (found this in help) but if I try to use
that directly, Intellisense won't accept it saying "Reference to a
non-shared member requires an object reference." so I declared a variable as
a HttpServerUtility, but that fails at runtime with a null object reference
error.

So all I really want to do is be able to refer to my application root folder
from a vb class in my asp.net application. Not sure if I'm close in my
approach above, but if someone could give me a hint on how to do this I'd
really appreciate it. I just want it to work like Server.MapPath in a
codebehind page. Thanks!

Matt
 
C

Cowboy \(Gregory A. Beamer\) [MVP]

Server and HttpServerUtility are equivalent overall. The best way to use it
is to create a HttpServerUtility object and pass it in to your class file.
Server is an instantiated copy of HttpServerUtility; you just do not see the
instantiation.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
M

MattB

Thanks for the response!
If possible I'd like to not have to pass it in. Is there anyway I can
instantiate it in my vb class so I don't need to modify code that calls this
class elsewhere?
 
K

Kevin Spencer

You don't need to instantiate it. You need to reference it. It already
exists in the HttpContext of the Current Request:

string s = System.Web.HttpContext.Current.Server.MapPath(url);

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

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

Similar Threads

Server.MapPath 2
Server.MapPath from a vb class 4
using Server.MapPath in vb class 3
.VB converting to C# 0
Server.mappath 7
Alternative of server.mappath() in my class 3
Server.Mappath 1
Server.MapPath("/"); 1

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top