HttpContext.Current.Server.MapPath throwing error: Object reference not set to an instance of an object

Dev
Dev
14 Points
7 Posts

I'm using following code in a class:

string tempPath = HttpContext.Current.Server.MapPath("/temp/")

But getting following error (some time working):

Object reference not set to an instance of an object.
Views: 61
Total Answered: 1
Total Marked As Answer: 0
Posted On: 23-Jan-2021 23:40

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Moderator
1252 Points
221 Posts
         

Server.MapPath is slow. Use HttpRuntime.AppDomainAppPath. As long as your web site is running, this property will be always available to you.

Use it like this:

string tempPath = Path.Combine(HttpRuntime.AppDomainAppPath, "/temp/");
Posted On: 26-Jan-2021 01:19
 Log In to Chat