How can I use Server.MapPath() from global.asax?

John Bubriski picture John Bubriski · Jun 1, 2009 · Viewed 35.8k times · Source

I need to use Server.MapPath() to combine some files path that I store in the web.config.

However, since Server.MapPath() relies on the current HttpContext (I think), I am unable to do this. When trying to use the method, even though its "available", I get the following exception:

Server operation is not available in this context.

Is there another method that can map a web root relative directory such as ~/App_Data/ to the full physical path such as C:\inetpub\wwwroot\project\App_data\ ?

Answer

Corbin March picture Corbin March · Jun 1, 2009

You could try System.Web.Hosting.HostingEnvironment.MapPath().

No HttpContext required.