using Server.MapPath

K

Kian Goh

Hi,

Can somebody tell me why can't I use
string strPath = Server.MapPath(Request.ApplicationPath);
in
protected void Application_Start(...)

It compiles ok but an unhandled exception occurred during the execution.
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

I have no problem using the above code in private void Page_Load(...)

Thanks,
Kian
 
S

Steve C. Orr, MCSD

There is no Request yet when your application is starting up.
The Request object is not yet in scope.
 
K

Kian Goh

Thanks Steve.

I intended to implement a counter for my webpage. I store my counter value
in a text file located in the ApplicationPath. I will read the counter from
the file on application start up and increment the value on every new
session created. Counter will be written on to the file at the end of
application.

If there is no Request yet during the application start up, is there any way
for me to know the ApplicationPath. At the moment, I am using hard coded
path.

OR let me know if there is other way to implement a counter.

Thanks,
Kian

====================================
INSIDE protected void Application_Start(...)
====================================
StreamReader sr = File.OpenText("d:\\kk\\omac\\omacCounter.txt");
Application["Counter"] =
System.Convert.ToInt16(sr.ReadToEnd().ToString());
====================================
 
S

Steve C. Orr, MCSD

Well one solution would be to store the path in your web.config file.
Then in your code you can get the path like this:
Dim sMyPath As String = ConfigurationSettings.AppSettings("AppPath")
Here's more info:
http://msdn.microsoft.com/library/d...onfigurationsettingsclassappsettingstopic.asp

But a better solution might be to user the Server.Mappath method instead. I
believe the Server object is in scope in the Application_Start procedure.
Here's more info:
http://msdn.microsoft.com/library/d...stemwebhttpserverutilityclassmappathtopic.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net


Kian Goh said:
Thanks Steve.

I intended to implement a counter for my webpage. I store my counter value
in a text file located in the ApplicationPath. I will read the counter from
the file on application start up and increment the value on every new
session created. Counter will be written on to the file at the end of
application.

If there is no Request yet during the application start up, is there any way
for me to know the ApplicationPath. At the moment, I am using hard coded
path.

OR let me know if there is other way to implement a counter.

Thanks,
Kian

====================================
INSIDE protected void Application_Start(...)
====================================
StreamReader sr = File.OpenText("d:\\kk\\omac\\omacCounter.txt");
Application["Counter"] =
System.Convert.ToInt16(sr.ReadToEnd().ToString());
====================================



Steve C. Orr said:
There is no Request yet when your application is starting up.
The Request object is not yet in scope.
 
Joined
Sep 8, 2009
Messages
1
Reaction score
0
Server.MapPath only works on actions

Why can't I use Server.MapPath method on the Controller constructor or in the business logic layer that is called by the controller actions?

I need the physical path correspondent to the virtual path "/" in my ASP.NET MVC application.

If you can please help... Can't find anything!

Thank you!
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top