How to get the physical location of an ASP.NET web application without using HttpContext.Current?
I've found myself having a requirement to configure log4net based on a file relative to the physical location of the running ASP.NET web application. We like to start the logger as early as possible, so Application_Start
seems a proper place. In IIS6, this works fine and has been r开发者_运维技巧unning for ages, but now we moved to IIS7 and this won't work anymore:
string absolutePath = HttpContext.Current.Request.PhysicalApplicationPath;
because the HttpContext.Current is not available in many global.asax
(Application, Session) events. This is old news, we all know it raises the now infamous Request is not available in this context error. We don't want to move back to Classic Mode.
Now, the question is simple: without using HttpContext
, is it possible to find the physical location of the currently running web application instance?
Try HttpRuntime.AppDomainAppPath. For more info, read IIS7 Integrated mode: Request is not available in this context exception in Application_Start posted by Mike Volodarsky.
As an alternative answer to my own question, I'd like to add that Server.MapPath()
works during the Application_Start
event as well. The application domain path would then be equal to Server.MapPath("~")
, but it's handier for mapping relative paths, removing the burden of concatenation (what MapPath was meant for to begin with).
精彩评论