From a managed class library, I'd like to find out whether the currently executing application is an ASP.NET web application (web forms or MVC) or not.
I have seen different approaches to do so, e.g. by checking one of the following:
System.Web.Hosting.HostingEnvironment.IsHosted == true
System.Web.HttpContext.Current != null
System.Web.HttpRuntime.AppDomainAppId != null
System.Web.HttpRuntime.Cache != null
- checking for a
web.config
file (note: I don't think this is reliable)
The question is which approach should I be using? Are some of them invalid (i.e. might they return true even when running in a windows app) or are all of them equal?
Update / clarification (sorry if my question wasn't clear enough):
- I have a managed class library (.net code) which is run by a .net application (obviously)
- this "host application" can either be an ASP.NET application (e.g. web forms or MVC) or a windows application (e.g. console or win forms)
- my question is: is there a way to reliably determine from within my class library (at runtime) whether it is running as part of an ASP.NET application?
Note: I know I could implement a different solution (e.g. see comments below or Tomas Lycken's answer), but that is not the point of this question. The class library is already existing, and I'd like to change as little code as possible!