Getting ApplicationPath (or equivelent) in Application_Start handler

Discussion in 'ASP .Net' started by bryan@newsgroups.nospam, Oct 18, 2005.

  1. Guest

    Is there any way I can get the application path (the one returned by
    Request.ApplicationPath) in the Application_Start method in
    Global.asax? Request is not valid there. On a related note, is there a
    way to get it from a static method in an aspx page, or in the class
    (static) constructor for an aspx page?

    Thanks,
    Bryan
     
    , Oct 18, 2005
    #1
    1. Advertising

  2. Hi Bryan,

    Welcome to ASPNET newsgroup.
    As for getting asp.net application's root app path in Application_Start
    event, I think we can consider the following two means:

    1. Use the HttpContext.Current.Server object 's MapPath with the "~/" path
    expression to get the app root's physical path. For example:

    string path = HttpContext.Current.Server.MapPath("~/");

    2. AS far as in the 1.x version, since each asp.net application is hosted
    in a single .net APPDomain, and the AppDomain's BaseDirectory is set as the
    Asp.net application's web root, so we can also use the following statement
    to get the application's base dir also:

    string basedir = AppDomain.CurrentDomain.BaseDirectory;

    Hope helps. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)





    --------------------
    | From: am
    | Subject: Getting ApplicationPath (or equivelent) in Application_Start
    handler
    | Date: Tue, 18 Oct 2005 00:10:48 -0500
    | Message-ID: <>
    | X-Newsreader: Forte Agent 3.1/32.783
    | MIME-Version: 1.0
    | Content-Type: text/plain; charset=us-ascii
    | Content-Transfer-Encoding: 7bit
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | NNTP-Posting-Host: adsl-68-253-201-186.dsl.emhril.ameritech.net
    68.253.201.186
    | Lines: 1
    | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
    | Xref: TK2MSFTNGXA01.phx.gbl
    microsoft.public.dotnet.framework.aspnet:131992
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | Is there any way I can get the application path (the one returned by
    | Request.ApplicationPath) in the Application_Start method in
    | Global.asax? Request is not valid there. On a related note, is there a
    | way to get it from a static method in an aspx page, or in the class
    | (static) constructor for an aspx page?
    |
    | Thanks,
    | Bryan
    |
     
    Steven Cheng[MSFT], Oct 18, 2005
    #2
    1. Advertising

  3. Guest

    Steven -
    Thanks, but that's not quite what I need. If I have an ASP.NET
    application called foo, Request.ApplicationPath returs /foo. I'm not
    looking for the physical path on the machine, but the name of the
    virtual application root. Any way to do this?

    Thanks,
    Bryan

    On Tue, 18 Oct 2005 08:20:26 GMT, (Steven
    Cheng[MSFT]) wrote:
    >Hi Bryan,
    >
    >Welcome to ASPNET newsgroup.
    >As for getting asp.net application's root app path in Application_Start
    >event, I think we can consider the following two means:
    >
    >1. Use the HttpContext.Current.Server object 's MapPath with the "~/" path
    >expression to get the app root's physical path. For example:
    >
    >string path = HttpContext.Current.Server.MapPath("~/");
    >
    >2. AS far as in the 1.x version, since each asp.net application is hosted
    >in a single .net APPDomain, and the AppDomain's BaseDirectory is set as the
    >Asp.net application's web root, so we can also use the following statement
    >to get the application's base dir also:
    >
    >string basedir = AppDomain.CurrentDomain.BaseDirectory;
    >
    >Hope helps. Thanks,
    >
    >Steven Cheng
    >Microsoft Online Support
    >
    >Get Secure! www.microsoft.com/security
    >(This posting is provided "AS IS", with no warranties, and confers no
    >rights.)
    >
    >
    >
    >
    >
    >--------------------
    >| From: am
    >| Subject: Getting ApplicationPath (or equivelent) in Application_Start
    >handler
    >| Date: Tue, 18 Oct 2005 00:10:48 -0500
    >| Message-ID: <>
    >| X-Newsreader: Forte Agent 3.1/32.783
    >| MIME-Version: 1.0
    >| Content-Type: text/plain; charset=us-ascii
    >| Content-Transfer-Encoding: 7bit
    >| Newsgroups: microsoft.public.dotnet.framework.aspnet
    >| NNTP-Posting-Host: adsl-68-253-201-186.dsl.emhril.ameritech.net
    >68.253.201.186
    >| Lines: 1
    >| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
    >| Xref: TK2MSFTNGXA01.phx.gbl
    >microsoft.public.dotnet.framework.aspnet:131992
    >| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    >|
    >| Is there any way I can get the application path (the one returned by
    >| Request.ApplicationPath) in the Application_Start method in
    >| Global.asax? Request is not valid there. On a related note, is there a
    >| way to get it from a static method in an aspx page, or in the class
    >| (static) constructor for an aspx page?
    >|
    >| Thanks,
    >| Bryan
    >|
     
    , Oct 18, 2005
    #3
  4. re:
    > Any way to do this?


    There is a way, but not from Application_Start.
    As you already know, the Request object is not available in it.

    Can you use the Init event ? ( Overriding it with OnInit ), or the Page_Load event ?
    Either of them would allow you to Request the application's virtual path.

    Also, unless you want the data to be portable to other boxes, you could
    simply set an Application variable which holds the Application's virtual path
    ( kinda klunky, I know, but it would get the job done... ).




    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/
    ======================================
    <> wrote in message
    news:...
    > Steven -
    > Thanks, but that's not quite what I need. If I have an ASP.NET
    > application called foo, Request.ApplicationPath returs /foo. I'm not
    > looking for the physical path on the machine, but the name of the
    > virtual application root. Any way to do this?
    >
    > Thanks,
    > Bryan
    >
    > On Tue, 18 Oct 2005 08:20:26 GMT, (Steven
    > Cheng[MSFT]) wrote:
    >>Hi Bryan,
    >>
    >>Welcome to ASPNET newsgroup.
    >>As for getting asp.net application's root app path in Application_Start
    >>event, I think we can consider the following two means:
    >>
    >>1. Use the HttpContext.Current.Server object 's MapPath with the "~/" path
    >>expression to get the app root's physical path. For example:
    >>
    >>string path = HttpContext.Current.Server.MapPath("~/");
    >>
    >>2. AS far as in the 1.x version, since each asp.net application is hosted
    >>in a single .net APPDomain, and the AppDomain's BaseDirectory is set as the
    >>Asp.net application's web root, so we can also use the following statement
    >>to get the application's base dir also:
    >>
    >>string basedir = AppDomain.CurrentDomain.BaseDirectory;
    >>
    >>Hope helps. Thanks,
    >>
    >>Steven Cheng
    >>Microsoft Online Support
    >>
    >>Get Secure! www.microsoft.com/security
    >>(This posting is provided "AS IS", with no warranties, and confers no
    >>rights.)
    >>
    >>
    >>
    >>
    >>
    >>--------------------
    >>| From: am
    >>| Subject: Getting ApplicationPath (or equivelent) in Application_Start
    >>handler
    >>| Date: Tue, 18 Oct 2005 00:10:48 -0500
    >>| Message-ID: <>
    >>| X-Newsreader: Forte Agent 3.1/32.783
    >>| MIME-Version: 1.0
    >>| Content-Type: text/plain; charset=us-ascii
    >>| Content-Transfer-Encoding: 7bit
    >>| Newsgroups: microsoft.public.dotnet.framework.aspnet
    >>| NNTP-Posting-Host: adsl-68-253-201-186.dsl.emhril.ameritech.net
    >>68.253.201.186
    >>| Lines: 1
    >>| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
    >>| Xref: TK2MSFTNGXA01.phx.gbl
    >>microsoft.public.dotnet.framework.aspnet:131992
    >>| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    >>|
    >>| Is there any way I can get the application path (the one returned by
    >>| Request.ApplicationPath) in the Application_Start method in
    >>| Global.asax? Request is not valid there. On a related note, is there a
    >>| way to get it from a static method in an aspx page, or in the class
    >>| (static) constructor for an aspx page?
    >>|
    >>| Thanks,
    >>| Bryan
    >>|
     
    Juan T. Llibre, Oct 18, 2005
    #4
  5. Hi Bryan,

    Thanks for your response.
    Based on my further test, we can try using the HttpContext.Current.Request
    to access the HttpRequest object since Application_Start is also fired by
    a certain client request. However, as Juan has mentioned, generally
    speaking Request is invisible in application_Start event, you'd better add
    some code to check whether the HttpContext's member property being null so
    as to insure no exception will occured. Also, if possible, you can
    consider Juan's suggestion on move the App Path query to Page's certain
    event (cache them once).

    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)


    --------------------
    | From: "Juan T. Llibre" <>
    | References: <>
    <cw#>
    <>
    | Subject: Re: Getting ApplicationPath (or equivelent) in Application_Start
    handler
    | Date: Tue, 18 Oct 2005 08:53:17 -0400
    | Lines: 94
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
    | X-RFC2646: Format=Flowed; Original
    | Message-ID: <ei3lhL#>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | NNTP-Posting-Host: 222stb33.codetel.net.do 64.32.114.222
    | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
    | Xref: TK2MSFTNGXA01.phx.gbl
    microsoft.public.dotnet.framework.aspnet:132068
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | re:
    | > Any way to do this?
    |
    | There is a way, but not from Application_Start.
    | As you already know, the Request object is not available in it.
    |
    | Can you use the Init event ? ( Overriding it with OnInit ), or the
    Page_Load event ?
    | Either of them would allow you to Request the application's virtual path.
    |
    | Also, unless you want the data to be portable to other boxes, you could
    | simply set an Application variable which holds the Application's virtual
    path
    | ( kinda klunky, I know, but it would get the job done... ).
    |
    |
    |
    |
    | 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/
    | ======================================
    | <> wrote in message
    | news:...
    | > Steven -
    | > Thanks, but that's not quite what I need. If I have an ASP.NET
    | > application called foo, Request.ApplicationPath returs /foo. I'm not
    | > looking for the physical path on the machine, but the name of the
    | > virtual application root. Any way to do this?
    | >
    | > Thanks,
    | > Bryan
    | >
    | > On Tue, 18 Oct 2005 08:20:26 GMT, (Steven
    | > Cheng[MSFT]) wrote:
    | >>Hi Bryan,
    | >>
    | >>Welcome to ASPNET newsgroup.
    | >>As for getting asp.net application's root app path in Application_Start
    | >>event, I think we can consider the following two means:
    | >>
    | >>1. Use the HttpContext.Current.Server object 's MapPath with the "~/"
    path
    | >>expression to get the app root's physical path. For example:
    | >>
    | >>string path = HttpContext.Current.Server.MapPath("~/");
    | >>
    | >>2. AS far as in the 1.x version, since each asp.net application is
    hosted
    | >>in a single .net APPDomain, and the AppDomain's BaseDirectory is set as
    the
    | >>Asp.net application's web root, so we can also use the following
    statement
    | >>to get the application's base dir also:
    | >>
    | >>string basedir = AppDomain.CurrentDomain.BaseDirectory;
    | >>
    | >>Hope helps. Thanks,
    | >>
    | >>Steven Cheng
    | >>Microsoft Online Support
    | >>
    | >>Get Secure! www.microsoft.com/security
    | >>(This posting is provided "AS IS", with no warranties, and confers no
    | >>rights.)
    | >>
    | >>
    | >>
    | >>
    | >>
    | >>--------------------
    | >>| From: am
    | >>| Subject: Getting ApplicationPath (or equivelent) in Application_Start
    | >>handler
    | >>| Date: Tue, 18 Oct 2005 00:10:48 -0500
    | >>| Message-ID: <>
    | >>| X-Newsreader: Forte Agent 3.1/32.783
    | >>| MIME-Version: 1.0
    | >>| Content-Type: text/plain; charset=us-ascii
    | >>| Content-Transfer-Encoding: 7bit
    | >>| Newsgroups: microsoft.public.dotnet.framework.aspnet
    | >>| NNTP-Posting-Host: adsl-68-253-201-186.dsl.emhril.ameritech.net
    | >>68.253.201.186
    | >>| Lines: 1
    | >>| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
    | >>| Xref: TK2MSFTNGXA01.phx.gbl
    | >>microsoft.public.dotnet.framework.aspnet:131992
    | >>| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    | >>|
    | >>| Is there any way I can get the application path (the one returned by
    | >>| Request.ApplicationPath) in the Application_Start method in
    | >>| Global.asax? Request is not valid there. On a related note, is there a
    | >>| way to get it from a static method in an aspx page, or in the class
    | >>| (static) constructor for an aspx page?
    | >>|
    | >>| Thanks,
    | >>| Bryan
    | >>|
    |
    |
    |
     
    Steven Cheng[MSFT], Oct 19, 2005
    #5
  6. Guest

    Steven, Juan -
    Thanks for the insights. Between Page_Load and a static
    constructor, I can make it work just fine.

    Cheers,
    Bryan
     
    , Oct 19, 2005
    #6
  7. tdavisjr Guest

    You should be able to use Request.ApplicationPath however, you may have
    to access it through the HttpContext Object like:

    HttpContext.Current.Request.ApplicationPath
     
    tdavisjr, Oct 19, 2005
    #7
  8. Good news !

    Glad you found a solution...



    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/
    ======================================
    <> wrote in message
    news:...
    > Steven, Juan -
    > Thanks for the insights. Between Page_Load and a static
    > constructor, I can make it work just fine.
    >
    > Cheers,
    > Bryan
     
    Juan T. Llibre, Oct 19, 2005
    #8
  9. Cheers Bryan,

    Have a good day!


    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | From: "Juan T. Llibre" <>
    | References: <>
    <cw#>
    <>
    <ei3lhL#>
    <>
    <>
    | Subject: Re: Getting ApplicationPath (or equivelent) in Application_Start
    handler
    | Date: Wed, 19 Oct 2005 12:12:05 -0400
    | Lines: 20
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
    | X-RFC2646: Format=Flowed; Original
    | Message-ID: <OHn#>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | NNTP-Posting-Host: 222stb33.codetel.net.do 64.32.114.222
    | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
    | Xref: TK2MSFTNGXA01.phx.gbl
    microsoft.public.dotnet.framework.aspnet:132492
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | Good news !
    |
    | Glad you found a solution...
    |
    |
    |
    | 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/
    | ======================================
    | <> wrote in message
    | news:...
    | > Steven, Juan -
    | > Thanks for the insights. Between Page_Load and a static
    | > constructor, I can make it work just fine.
    | >
    | > Cheers,
    | > Bryan
    |
    |
    |
     
    Steven Cheng[MSFT], Oct 20, 2005
    #9
    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. Jim Corey
    Replies:
    0
    Views:
    967
    Jim Corey
    Nov 21, 2003
  2. Leo Jay
    Replies:
    16
    Views:
    498
    Derek Martin
    Jun 19, 2008
  3. Jules
    Replies:
    40
    Views:
    416
    Jules
    Dec 28, 2005
  4. John Wilkinson

    Perl Equivelent of C++ program

    John Wilkinson, Mar 22, 2007, in forum: Perl Misc
    Replies:
    6
    Views:
    122
    -berlin.de
    Mar 23, 2007
  5. Graham Drabble

    Perl equivelent of "links -dump"

    Graham Drabble, Oct 18, 2009, in forum: Perl Misc
    Replies:
    2
    Views:
    99
    Graham Drabble
    Oct 18, 2009
Loading...

Share This Page