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

T

teo

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?
 
M

Mark Fitzpatrick

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.
 
T

teo

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)
 
J

Juan T. Llibre

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.
 
S

Samuel R. Neff

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
 
T

teo

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?
 
J

Juan T. Llibre

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.
 
T

teo

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
 

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

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top