471,573 Members | 1,032 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Getting ApplicationPath (or equivelent) in Application_Start handler

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
Nov 19 '05 #1
8 8303
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: br***@newsgroups.nospam
| Subject: Getting ApplicationPath (or equivelent) in Application_Start
handler
| Date: Tue, 18 Oct 2005 00:10:48 -0500
| Message-ID: <qn********************************@4ax.com>
| 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!TK2MSFT NGP10.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
|

Nov 19 '05 #2
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, st*****@online.microsoft.com (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: br***@newsgroups.nospam
| Subject: Getting ApplicationPath (or equivelent) in Application_Start
handler
| Date: Tue, 18 Oct 2005 00:10:48 -0500
| Message-ID: <qn********************************@4ax.com>
| 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!TK2MSFT NGP10.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
|

Nov 19 '05 #3
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/
======================================
<br***@newsgroups.nospam> wrote in message
news:dm********************************@4ax.com... 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, st*****@online.microsoft.com (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: br***@newsgroups.nospam
| Subject: Getting ApplicationPath (or equivelent) in Application_Start
handler
| Date: Tue, 18 Oct 2005 00:10:48 -0500
| Message-ID: <qn********************************@4ax.com>
| 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!TK2MSFT NGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:13199 2
| 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
|

Nov 19 '05 #4
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" <no***********@nowhere.com>
| References: <qn********************************@4ax.com>
<cw**************@TK2MSFTNGXA01.phx.gbl>
<dm********************************@4ax.com>
| 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: <ei*************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 222stb33.codetel.net.do 64.32.114.222
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.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/
| ======================================
| <br***@newsgroups.nospam> wrote in message
| news:dm********************************@4ax.com...
| > 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, st*****@online.microsoft.com (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: br***@newsgroups.nospam
| >>| Subject: Getting ApplicationPath (or equivelent) in Application_Start
| >>handler
| >>| Date: Tue, 18 Oct 2005 00:10:48 -0500
| >>| Message-ID: <qn********************************@4ax.com>
| >>| 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!TK2MSFT NGP10.phx.gbl
| >>| Xref: TK2MSFTNGXA01.phx.gbl
| >>microsoft.public.dotnet.framework.aspnet:13199 2
| >>| 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
| >>|
|
|
|

Nov 19 '05 #5
Steven, Juan -
Thanks for the insights. Between Page_Load and a static
constructor, I can make it work just fine.

Cheers,
Bryan

Nov 19 '05 #6
You should be able to use Request.ApplicationPath however, you may have
to access it through the HttpContext Object like:

HttpContext.Current.Request.ApplicationPath

Nov 19 '05 #7
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/
======================================
<br***@newsgroups.nospam> wrote in message
news:vs********************************@4ax.com...
Steven, Juan -
Thanks for the insights. Between Page_Load and a static
constructor, I can make it work just fine.

Cheers,
Bryan

Nov 19 '05 #8
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" <no***********@nowhere.com>
| References: <qn********************************@4ax.com>
<cw**************@TK2MSFTNGXA01.phx.gbl>
<dm********************************@4ax.com>
<ei*************@tk2msftngp13.phx.gbl>
<Wp**************@TK2MSFTNGXA01.phx.gbl>
<vs********************************@4ax.com>
| 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: <OH**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 222stb33.codetel.net.do 64.32.114.222
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.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/
| ======================================
| <br***@newsgroups.nospam> wrote in message
| news:vs********************************@4ax.com...
| > Steven, Juan -
| > Thanks for the insights. Between Page_Load and a static
| > constructor, I can make it work just fine.
| >
| > Cheers,
| > Bryan
|
|
|

Nov 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Vadym Stetsyak | last post: by
2 posts views Thread by martin | last post: by
11 posts views Thread by KarimL | last post: by
1 post views Thread by James | last post: by
2 posts views Thread by CKKwan | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by

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.