如何获得一个ASP.NET Web应用程序的物理位置,而无需使用HttpContext.Current?如何获得、应用程序、物理、位置

2023-09-07 02:10:52 作者:你是傻逼

我发现有一个要求,根据文件相对于正在运行的ASP.NET Web应用程序的物理位置来配置log4net的自己。我们希望尽早启动记录,所以的Application_Start 似乎是一个合适的地方。在IIS6,这工作得很好,并已经运行了年龄,但现在我们搬到了IIS7,这将不再工作:

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;

由于HttpContext.Current是不是在很多的Global.asax (应用,会话)事件可用。这是老新闻,大家都知道它提出了一个现在已经臭名昭著的 请求不可用在这种情况下的错误。我们不想搬回到经典模式。

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.

现在,这个问题很简单:不使用的HttpContext ,是有可能找到当前正在运行的Web应用程序实例的物理位置

Now, the question is simple: without using HttpContext, is it possible to find the physical location of the currently running web application instance?

推荐答案

尝试HttpRuntime.AppDomainAppPath.欲了解更多信息,请阅读IIS7集成模式:请求是不是在的Application_Start这种情况下可以例外的张贴者迈克沃洛达尔斯基

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.