Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 838
Abel Avatar asked Jun 11 '10 20:06

Abel


People also ask

Where is HttpContext items stored?

It is stored in the memory of the server and the value is available for the entire lifetime of the request.

What is MapPath in asp net?

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.

What is the use of HttpContext in asp net?

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.


1 Answers

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.

like image 115
Alek Davis Avatar answered Sep 23 '22 13:09

Alek Davis