简体   繁体   English

如何在不使用HttpContext.Current的情况下获取ASP.NET Web应用程序的物理位置?

[英]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. 我发现自己需要根据相对于正在运行的ASP.NET Web应用程序的物理位置的文件来配置log4net。 We like to start the logger as early as possible, so Application_Start seems a proper place. 我们希望尽早启动记录器,因此Application_Start似乎是一个合适的位置。 In IIS6, this works fine and has been running for ages, but now we moved to IIS7 and this won't work anymore: 在IIS6中,这工作正常并且已经运行了很长时间,但现在我们转移到IIS7,这将不再起作用:

string absolutePath = HttpContext.Current.Request.PhysicalApplicationPath;

because the HttpContext.Current is not available in many global.asax (Application, Session) events. 因为HttpContext.Current在许多global.asax (应用程序,会话)事件中不可用。 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? 现在,问题很简单:不使用HttpContext ,是否可以找到当前运行的Web应用程序实例的物理位置?

As an alternative answer to my own question, I'd like to add that Server.MapPath() works during the Application_Start event as well. 作为我自己的问题的替代答案,我想补充一点, Server.MapPath()也在Application_Start事件期间工作。 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). 然后,应用程序域路径将等于Server.MapPath("~") ,但是它可以更方便地映射相对路径,消除连接负担(MapPath的开头意味着什么)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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