I have an ASP.NET WebApi application running in IIS. From a non-Request background worker thread I need to find out the base Uri of the application (e.g., https://machineName:port/applicationName/).
The usual suggestions do not work since the Request object is null in the case of a worker thread that was created in the Application_Start() method.
Update: I am adding the port to the environment-specific web.config file which gets merged at deployment time (as suggested by @Kenneth).
2 Answers 2
You can use the HostingEnvironment.ApplicationVirtualPath
property.
It's located in the System.Web.Hosting
-namespace of the System.Web assembly
This is the MSDN reference: https://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment(v=vs.110).aspx
2 Comments
I would suggest the same solution as @Kenneth.
Otherwise, I guess you could grab the value on App Start and capture it in memory for later usage.
Comments
Explore related questions
See similar questions with these tags.