Tuesday, March 13, 2012

server.mappath in a vb class

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!
MattServer 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!
****************************************
********
"MattB" <somedudeus@.yahoo.com> wrote in message
news:2pgv13Fknc98U1@.uni-berlin.de...
> 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
>
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?
Cowboy (Gregory A. Beamer) [MVP] wrote:
> 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.
>
> ****************************************
********
> Think Outside the Box!
> ****************************************
********
> "MattB" <somedudeus@.yahoo.com> wrote in message
> news:2pgv13Fknc98U1@.uni-berlin.de...
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.
"MattB" <somedudeus@.yahoo.com> wrote in message
news:2pgvuuFjph3aU1@.uni-berlin.de...
> 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?
> Cowboy (Gregory A. Beamer) [MVP] wrote:
>
>

0 comments:

Post a Comment