Jake Ashcraft's WebLog

Server.MapPath()

This always bites me in the ass. When you need to call Server.MapPath from a standard class, you must call it from the current context.

Example:

System.Web.HttpContext.Current.Server.MapPath(“somefile.xml“);

This is more of a note for my future reference.

 

Comments

Jeff Perrin said:

Thank God for Google. I was just bit by this one too. I won't post it to my blog as a reminder, though, so we'll see how long it is before it bites me again. ;)
# June 1, 2004 6:00 PM

TrackBack said:

^_^,Pretty Good!
# April 10, 2005 4:21 AM

Torben Christensen said:

Aaaah yes, of course!!! Thank you for posting this reminder....

# July 11, 2007 9:41 AM

Ramesh said:

yaaaa i got it .. thank god & google ...thank you verymuch  Jake Ashcraft's  :-)

# September 5, 2007 11:12 AM

Fred Abboud said:

Great posting (reminder) Saved me lots of research on ms website

# October 30, 2007 4:30 PM

Bhavesh said:

Thanks for this, very useful!

# December 30, 2007 11:08 AM

tribalpk said:

awesome. it's very hard with working web.config when using stupid access databases. it's cool, defining connectionstring dynamically, using the code without replacing any strings

# January 24, 2008 8:07 AM

Fozzie711 said:

Thanks for posting this...bit me too and now I can bite back.  :-)

# April 23, 2008 4:01 PM

NBstrat said:

This one has bite me more than once, thanks for the reminder.

# May 7, 2008 8:18 PM

Barbaros said:

Thanks for posting this, you save a lot of people s life with that :)

# May 13, 2008 10:50 AM

Alan said:

God I love little tips like this - 15 seconds of searching on Google. Problem solved. Many thanks.

# May 23, 2008 6:20 PM

jake ashcraft said:

Pingback from  jake ashcraft

# June 3, 2008 1:51 AM

pthalacker said:

Thank you.  I read the documentation on Server.MapPath 3 times and this information is conspicuously missing.

# October 17, 2008 11:58 AM

Peter said:

if only i could remember this...

# February 10, 2010 12:20 PM

Phani said:

Basics thanks for it.

# June 4, 2010 1:22 AM

rajiv said:

it says HttpContext is not a member of System.Web ..what to do

# June 13, 2010 1:39 AM

rtpHarry said:

rajiv, pretty late reply for you now but might help someone else on this page:

I think you need to add a reference to the system.web dll in the add reference > .net tab. If you are trying to use the from a class library then you might not have it in there yet?

# October 15, 2010 5:21 AM

Freddy wilson said:

Great hint. wish I found it two hours earlier

# January 14, 2011 11:00 AM

jqkunz said:

Thanks alot, cant remember everything all the time..... I have ran into this a dozen times and just don't recall

# July 8, 2011 8:04 PM

Narayana said:

System.Web.HttpContext.Current.Server.MapPath("EHS Tele Sales - Apr 2011_be.mdb");

Hi guys,

Thanks for the Google.

# July 20, 2011 6:23 AM

Deepesh said:

wow..simply amazing. You just made my life easier..Thanks

# September 27, 2011 6:39 PM

Devloper said:

No It's Not Work in Class File

# October 8, 2011 3:19 AM

hchandler said:

This was exactly what I was looking for.  Thanks!

# January 23, 2012 1:20 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)