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

Discussion in 'ASP .Net' started by teo, Dec 10, 2006.

  1. teo

    teo Guest

    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?
     
    teo, Dec 10, 2006
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    >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?
     
    Mark Fitzpatrick, Dec 10, 2006
    #2
    1. Advertising

  3. teo

    teo Guest

    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)
     
    teo, Dec 11, 2006
    #3
  4. 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" <> wrote in message news:...
    > 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)
    >
    >
     
    Juan T. Llibre, Dec 11, 2006
    #4
  5. 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 <> 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?
     
    Samuel R. Neff, Dec 11, 2006
    #5
  6. teo

    teo Guest

    >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?
     
    teo, Dec 12, 2006
    #6
  7. 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" <> wrote in message news:...
    > >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?
    >
     
    Juan T. Llibre, Dec 12, 2006
    #7
  8. teo

    teo Guest

    On Sun, 10 Dec 2006 17:55:29 +0100, teo <> 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
     
    teo, Dec 13, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sebastiano

    Server.MapPath in Global.asax

    Sebastiano, Apr 16, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    4,504
    Rashad Rivera
    Apr 16, 2004
  2. =?Utf-8?B?YmNoYXJsZXM=?=

    global.asax and global.asax.cs

    =?Utf-8?B?YmNoYXJsZXM=?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    894
    =?Utf-8?B?YmNoYXJsZXM=?=
    Oct 5, 2004
  3. Mark Rae

    Global.asax / Global.asax.cs in v2

    Mark Rae, May 23, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    3,197
    Mark Rae
    May 23, 2006
  4. Michael
    Replies:
    0
    Views:
    500
    Michael
    Feb 3, 2007
  5. =?Utf-8?B?dHBhcmtzNjk=?=

    server.mappath vs. request.mappath

    =?Utf-8?B?dHBhcmtzNjk=?=, Mar 10, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    1,197
    Juan T. Llibre
    Mar 11, 2007
Loading...

Share This Page