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

Dev
Dev
Member
40 Points
5 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: 2220
Total Answered: 1
Total Marked As Answer: 0
Posted On: 23-Jan-2021 23:40

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Teacher
4826 Points
23 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