473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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

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?
Dec 10 '06 #1
7 6224
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" <te*@inwind.itwrote in message
news:hg********************************@4ax.com...
>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?

Dec 10 '06 #2
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)
Dec 11 '06 #3
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" <te*@inwind.itwrote in message news:l3********************************@4ax.com...
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)


Dec 11 '06 #4

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 <te*@inwind.itwrote:
>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?
Dec 11 '06 #5
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?

Dec 12 '06 #6
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" <te*@inwind.itwrote in message news:7r********************************@4ax.com...
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?

Dec 12 '06 #7
teo
On Sun, 10 Dec 2006 17:55:29 +0100, teo <te*@inwind.itwrote:
>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
Dec 13 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Lamey | last post by:
Hi All, I have the following code but for some reason I cannot get the Session_OnEnd event to fire. I am trying to limit the amount of connections a browser session can have. Where the...
1
by: Steve | last post by:
I am trying to call a file delete function from inside Session_End in Global.asax.cs. However, everytime I step into my delete function from Session_End it jumps to the catch statement and I get...
3
by: Sebastiano | last post by:
I want to use Server.MapPath function in Global.asax because I want to save in an application variable the path of my database. Global.asax is in the root directory of my web folder and database is...
3
by: hansiman | last post by:
I use Application_Start in global.asax to set some physical folder paths ie.: Application("pdf") = "c:\www\<site>\pdf\" global.asax uses code behind. When I move the project from the dev to...
0
by: Rick Hein | last post by:
I've got a problem with an app I've been working on, the Caching object and events not firing correctly. In a nutshell: When I'm debugging, and I set a breakpoint in the removed item call back, the...
2
by: rgparkins | last post by:
Hi Guys Maybe a simple and easy solution to this, but I keep getting exception object not set to instance etc etc. I have a Timer that is initiated in Application_Start in Global.asax. This...
0
by: Michael | last post by:
Hi I have a problem related to global.asax and the Server.Mappath-Method. I collect all unhandled errors in the Application_Error in Global.asax. According to the type of error I...
10
by: Yehia A.Salam | last post by:
Hello, I was trying to connect to an Xml database, and I thought of loading the Xml Document in "Application_Start" so that the xml is loaded only once and then queried later as many times as...
12
by: =?Utf-8?B?QWxleCBNYWdoZW4=?= | last post by:
Hi. I am trying to maintain a list of people who are currently "online" in SQL. I do this by adding a simple entry to a simple PeopleOnline table whenever someone logs in to my site. If they...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.