简体   繁体   中英

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 running 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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM