I use a System.Timers.Timer
in my Asp.Net application and I need to use the HttpServerUtility.MapPath
method which seems to be only available via HttpContext.Current.Server.MapPath
. The problem is that HttpContext.Current
is null
when the Timer.Elapsed
event fires.
Is there another way to get a reference to a HttpServerUtility object ? I could inject it in my class' constructor. Is it safe ? How can I be sure it won't be Garbage Collected at the end of the current request?
Thanks!
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.
If Path starts with either a forward (/) or backward slash (\), the MapPath method returns a path as if Path were a full, virtual path. If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the . asp file being processed.
For example, Server. MapPath() allows a path of "files/file1. doc". It uses the current context to determine the path of the current page, for example, and then creates the relative path from there.
The methods and properties of the HttpServerUtility class are exposed through the intrinsic Server object provided by ASP.NET. Gets the server's computer name. Gets or sets the request time-out value in seconds. Clears the previous exception. Creates a server instance of a COM object identified by the object's programmatic identifier (ProgID).
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 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 MapPath method potentially contains sensitive information about the hosting environment. The return value should not be displayed to users.
The physical file path on the Web server that corresponds to path. The current HttpContext is null. path is a physical path, but a virtual path was expected. The following example shows how to retrieve the physical file of a relative virtual path. The code resides in the code-behind file for a web page and utilizes the default Server object.
It's possible to use HostingEnvironment.MapPath()
instead of HttpContext.Current.Server.MapPath()
I haven't tried it yet in a thread or timer event though.
Some (non viable) solutions I considered;
The only method I care about on HttpServerUtility
is MapPath
. So as an alternative I could use AppDomain.CurrentDomain.BaseDirectory
and build my paths from this. But this will fail if your app uses virtual directories (Mine does).
Another approach: Add all the paths I need to the the Global
class. Resolve these paths in Application_Start
.
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