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?
It is stored in the memory of the server and the value is available for the entire lifetime of the request.
Remarks. If path is null , the MapPath method returns the full physical path of the directory that contains the current request for the path. The relative path does not need to specify an existing file or folder for this method to return a value. However, you cannot specify a path outside of the Web application.
The HttpContext object constructed by the ASP.NET Core web server acts as a container for a single request. It stores the request and response information, such as the properties of request, request-related services, and any data to/from the request or errors, if there are any.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With