Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > [Global.asax] function Server.MapPath not available ?

Reply
Thread Tools

[Global.asax] function Server.MapPath not available ?

 
 
teo
Guest
Posts: n/a
 
      12-10-2006
I need to use the 'Server.MapPath' function
in the 'Session_End' event of the Global.asax file
(to reach a folder and the clean some temporary files up),

but it doesn't work:

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
Dim myPath As String = (Server.MapPath("../public/MyFolder/")
End Sub

Note:
when I precompile the app,
the Global.asax file will be inserted in the BIN folder
(and not in the usual root folder)
so it may be a further problem

Any idea?
 
Reply With Quote
 
 
 
 
Mark Fitzpatrick
Guest
Posts: n/a
 
      12-10-2006
Is the public director a level above the root? You may try creating a path
relative to the root for the mappath. If public is above the root, you could
then try:
Server.MapPath("../" & Request.ApplicationPath & "/public/MyFolder/")

Request.ApplicationPath will return the name for the virtual root so it
won't be pinned on the local directory as much. Are you sure thought that
it's the global.asax that's being created in the bin and not just the dll
for the Global.asax file?

A couple other things to keep in mind. If the sessionstate is not set to
inproc, then the on_end event is ignored. Also, not all functions are
available in the on_end event. It is mainly there to cleanup session
variables and not necessarily to perform other cleanup tasks. For example,
classic ASP developers often tried to run database operations in the on_end
event but database calls weren't allowed during on_end.


--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006



"teo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I need to use the 'Server.MapPath' function
> in the 'Session_End' event of the Global.asax file
> (to reach a folder and the clean some temporary files up),
>
> but it doesn't work:
>
> Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
> Dim myPath As String = (Server.MapPath("../public/MyFolder/")
> End Sub
>
> Note:
> when I precompile the app,
> the Global.asax file will be inserted in the BIN folder
> (and not in the usual root folder)
> so it may be a further problem
>
> Any idea?



 
Reply With Quote
 
 
 
 
teo
Guest
Posts: n/a
 
      12-11-2006
Mark,
I tried

the 'public' folder is here
www.mySite.it/public/myFolder
and
my .aspx page folder is here
www.mySite.it/PersonalApp/Default.aspx
also
the InProc is set on

unfortunately
'Server.MapPath' and 'Request.ApplicationPath'
from the Session_End
both return an empty string

- - -

Because Server.MapPath on the Session_Start does work ,
I tried to store the resulting string in a variable
to use it in the Session_End ,
but it seems that
the Global.asax file manages variable differently from usual
and it 'looses' its content
and nothing arrives in the Session_End event

- - -

I 'm stumped
(I'm performing my tasks
on the Session_ Start event,
but I don't like it because it consumes time)


 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      12-11-2006
From Stephen Walter's "ASP.NET Unleashed" :

Within an ASP.NET page, the Page object is the default object.

In many cases, if you do not specify an object when you call a method or access a property,
you are implicitly calling the method or accessing the property from the Page object.

For example, when you call the MapPath method (which maps virtual paths to physical paths),
you are actually calling the Page.MapPath method. Or, when you access the Cache property,
you are implicitly accessing the Page.Cache property.

The Global.asax file does not have the Page object as its default object.
This means that you cannot simply call a method such as MapPath and expect it to work.

Fortunately, in most cases, you can use a simple workaround.
Instead of calling the MapPath method, you can call the Context.MapPath method.
Or, instead of accessing the Page.Cache object, you can access the Context.Cache object.

If a familiar property or method does not work in the Global.asax file,
you should immediately try calling the method or property by using the Context object instead.

Try it.



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaņol : http://asp.net.do/foros/
===================================

"teo" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Mark,
> I tried
>
> the 'public' folder is here
> www.mySite.it/public/myFolder
> and
> my .aspx page folder is here
> www.mySite.it/PersonalApp/Default.aspx
> also
> the InProc is set on
>
> unfortunately
> 'Server.MapPath' and 'Request.ApplicationPath'
> from the Session_End
> both return an empty string
>
> - - -
>
> Because Server.MapPath on the Session_Start does work ,
> I tried to store the resulting string in a variable
> to use it in the Session_End ,
> but it seems that
> the Global.asax file manages variable differently from usual
> and it 'looses' its content
> and nothing arrives in the Session_End event
>
> - - -
>
> I 'm stumped
> (I'm performing my tasks
> on the Session_ Start event,
> but I don't like it because it consumes time)
>
>



 
Reply With Quote
 
Samuel R. Neff
Guest
Posts: n/a
 
      12-11-2006

MapPath translates a relative path based on the current url to an
absolute path. When Session_End is called it's based on a timer and
not a request, so there is no current path from which MapPath can
calculate a relative path.

Perhaps Path.Combine and Assembly.CodeBase can be used in your
situation.

HTH,

Sam


------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.



On Sun, 10 Dec 2006 17:55:29 +0100, teo <(E-Mail Removed)> wrote:

>I need to use the 'Server.MapPath' function
>in the 'Session_End' event of the Global.asax file
>(to reach a folder and the clean some temporary files up),
>
>but it doesn't work:
>
>Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
> Dim myPath As String = (Server.MapPath("../public/MyFolder/")
>End Sub
>
>Note:
>when I precompile the app,
>the Global.asax file will be inserted in the BIN folder
>(and not in the usual root folder)
>so it may be a further problem
>
>Any idea?


 
Reply With Quote
 
teo
Guest
Posts: n/a
 
      12-12-2006
>Fortunately, in most cases, you can use a simple workaround.
>Instead of calling the MapPath method, you can call the Context.MapPath method.
>Or, instead of accessing the Page.Cache object, you can access the Context.Cache object.

I tried,

maybe I always adopted a wrong syntax,
but I always saw the blu irregular underline
under my syntax;

I tried:
Context.MapPath
HttpContext
HttpApplication
and several other namespaces,
but always got the blu irregular underline
(and the app never started)

Do you know the exact fully namespaced syntax?

 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      12-12-2006
Session_OnEnd ( or Session_End ) does not support the Request, Response or Server objects.
The only built-in objects you can use are Session and Application.

So, instead of using a Server.MapPath directive in Session_End,
store the fully qualified path in an Application variable.

You'll have no problem using an Application variable
in Session _End, provided Session_End fires at all.

There's many circumstances in which Session_End does not fire.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaņol : http://asp.net.do/foros/
===================================
"teo" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> >Fortunately, in most cases, you can use a simple workaround.
>>Instead of calling the MapPath method, you can call the Context.MapPath method.
>>Or, instead of accessing the Page.Cache object, you can access the Context.Cache object.

> I tried,
>
> maybe I always adopted a wrong syntax,
> but I always saw the blu irregular underline
> under my syntax;
>
> I tried:
> Context.MapPath
> HttpContext
> HttpApplication
> and several other namespaces,
> but always got the blu irregular underline
> (and the app never started)
>
> Do you know the exact fully namespaced syntax?
>



 
Reply With Quote
 
teo
Guest
Posts: n/a
 
      12-13-2006
On Sun, 10 Dec 2006 17:55:29 +0100, teo <(E-Mail Removed)> wrote:

>I need to use the 'Server.MapPath' function
>in the 'Session_End' event of the Global.asax file
>(to reach a folder and the clean some temporary files up),
>
>but it doesn't work:
>
>Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
> Dim myPath As String = (Server.MapPath("../public/MyFolder/")
>End Sub



I found a workaround:

because Server.MapPath does work on the Session_Start event,
I retrieve it in this event, store it in a module variable,
and then use this variable in the Session_End event,

but, important thing:

the variable has not to be declared as Dim myVariable
because it will not work,
but it has to be declared has Shared myVariable


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
libmsi.a import library from wine, and header files available(entirely free software),available for python-win32 builds under msys+wine Luke Kenneth Casson Leighton Python 0 01-18-2009 08:46 PM
How to find the total and free memory available in RAM. and used & free space available in each disk? kathirvel.com@gmail.com Java 1 11-02-2006 02:31 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
RE: Web page is not available - "The Web page you requested is not available offline. To view this page, click Connect" =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?= ASP .Net 0 06-07-2004 07:11 AM
Re: Web page is not available - "The Web page you requested is not available offline. To view this page, click Connect" Natty Gur ASP .Net 0 06-06-2004 05:46 AM



Advertisments