如何获得一个ASP.NET Web应用程序的物理位置,而无需使用HttpContext.Current? [英] How to get the physical location of an ASP.NET web application without using HttpContext.Current?

查看:258
本文介绍了如何获得一个ASP.NET Web应用程序的物理位置,而无需使用HttpContext.Current?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有一个要求,根据文件相对于正在运行的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 (应用,会话)事件可用。这是老新闻,大家都知道它提出了一个现在已经臭名昭著的<一href="http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx"相对=nofollow> 请求不可用在这种情况下的错误。我们不想搬回到经典模式。

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?

推荐答案

尝试<一href="http://msdn.microsoft.com/en-us/library/system.web.httpruntime.appdomainapppath.aspx">HttpRuntime.AppDomainAppPath.欲了解更多信息,请阅读<一个href="http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx">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.

这篇关于如何获得一个ASP.NET Web应用程序的物理位置,而无需使用HttpContext.Current?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆