Tuesday, March 13, 2012

server.mappath

In the code behind server.mappath works fine, yet in a class I get 'Server
operation is not available in this context'. System.Web.HttpApplication is
inherited in the class, why is this so?

Colin.Colin,

It's not enough to just inherit it. The page fills many properties of the
class. If you pass the current instance of the HttpApplication object into
your class then you'll be able to access it the way you want.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Colin" <c@...cl> wrote in message
news:u2uu60O4FHA.3540@.TK2MSFTNGP10.phx.gbl...
> In the code behind server.mappath works fine, yet in a class I get 'Server
> operation is not available in this context'. System.Web.HttpApplication
> is
> inherited in the class, why is this so?
> Colin.
Ah, I see. Classes are new'ish to me. How did I pass HttpApplication to
the class when instantiated? I'm using Vb.Net.

Colin.

"S. Justin Gengo" <justin@.[no_spam_please]aboutfortunate.com> wrote in
message news:ehY8jOP4FHA.2600@.tk2msftngp13.phx.gbl...
Colin,

It's not enough to just inherit it. The page fills many properties of the
class. If you pass the current instance of the HttpApplication object into
your class then you'll be able to access it the way you want.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Colin" <c@...cl> wrote in message
news:u2uu60O4FHA.3540@.TK2MSFTNGP10.phx.gbl...
> In the code behind server.mappath works fine, yet in a class I get 'Server
> operation is not available in this context'. System.Web.HttpApplication
> is
> inherited in the class, why is this so?
> Colin.
Inside a class you could use:
System.Web.HttpContext.Current.Server.MapPath("~/mydir/")

"Colin" <c@...cl> schreef in bericht
news:u2uu60O4FHA.3540@.TK2MSFTNGP10.phx.gbl...
> In the code behind server.mappath works fine, yet in a class I get 'Server
> operation is not available in this context'. System.Web.HttpApplication
> is
> inherited in the class, why is this so?
> Colin.
Colin,

If you always want it to be included then require it as a parameter of your
class' sub new:

Private _HttpApplication

Public Sub New(ByRef httpApplication As System.Web.HttpApplication)
_HttpApplication = httpApplication
End Sub

If you just need it for one method inside of your class declare it as a
parameter of that method itself

Public Sub MyMethod(ByRef httpApplication As System.Web.HttpApplication)

End Sub

You should use ByRef instead of ByVal so that if you store something in the
HttpApplication.Context object or make other changes to it's properties they
will be reflected throughout the application.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Colin" <c@...cl> wrote in message
news:OEeJS0P4FHA.632@.TK2MSFTNGP10.phx.gbl...
> Ah, I see. Classes are new'ish to me. How did I pass HttpApplication to
> the class when instantiated? I'm using Vb.Net.
> Colin.
>
> "S. Justin Gengo" <justin@.[no_spam_please]aboutfortunate.com> wrote in
> message news:ehY8jOP4FHA.2600@.tk2msftngp13.phx.gbl...
> Colin,
> It's not enough to just inherit it. The page fills many properties of the
> class. If you pass the current instance of the HttpApplication object into
> your class then you'll be able to access it the way you want.
> --
> Sincerely,
> S. Justin Gengo, MCP
> Web Developer / Programmer
> www.aboutfortunate.com
> "Out of chaos comes order."
> Nietzsche
> "Colin" <c@...cl> wrote in message
> news:u2uu60O4FHA.3540@.TK2MSFTNGP10.phx.gbl...
>> In the code behind server.mappath works fine, yet in a class I get
>> 'Server
>> operation is not available in this context'. System.Web.HttpApplication
>> is
>> inherited in the class, why is this so?
>>
>> Colin.
>>
>>

0 comments:

Post a Comment