Getting ApplicationPath (or equivelent) in Application_Start handler

B

bryan

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
 
S

Steven Cheng[MSFT]

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: (e-mail address removed)
| Subject: Getting ApplicationPath (or equivelent) in Application_Start
handler
| Date: Tue, 18 Oct 2005 00:10:48 -0500
| Message-ID: <[email protected]>
| 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
|
 
B

bryan

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
 
J

Juan T. Llibre

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

Steven Cheng[MSFT]

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" <[email protected]>
| References: <[email protected]>
<cw#[email protected]>
<[email protected]>
| 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#[email protected]>
| 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/
| ======================================
| | > 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, (e-mail address removed) (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: (e-mail address removed)
| >>| Subject: Getting ApplicationPath (or equivelent) in Application_Start
| >>handler
| >>| Date: Tue, 18 Oct 2005 00:10:48 -0500
| >>| Message-ID: <[email protected]>
| >>| 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
| >>|
|
|
|
 
B

bryan

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

Cheers,
Bryan
 
T

tdavisjr

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

HttpContext.Current.Request.ApplicationPath
 
S

Steven Cheng[MSFT]

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" <[email protected]>
| References: <[email protected]>
<cw#[email protected]>
<[email protected]>
<ei3lhL#[email protected]>
<[email protected]>
<[email protected]>
| 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#[email protected]>
| 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/
| ======================================
| | > Steven, Juan -
| > Thanks for the insights. Between Page_Load and a static
| > constructor, I can make it work just fine.
| >
| > Cheers,
| > Bryan
|
|
|
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top