Skip to content

Instantly share code, notes, and snippets.

@jarrettmeyer
Created May 25, 2012 15:26
Show Gist options
  • Select an option

  • Save jarrettmeyer/2788772 to your computer and use it in GitHub Desktop.

Select an option

Save jarrettmeyer/2788772 to your computer and use it in GitHub Desktop.
Determine if there is a current HTTP context without referencing System.Web
public static bool HasHttpContext
{
get
{
try
{
var assembly = Assembly.Load("System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
Type httpContextType = assembly.GetType("System.Web.HttpContext");
PropertyInfo currentProperty = httpContextType.GetProperty("Current");
object value = currentProperty.GetValue(null, null);
bool hasHttpContext = value != null;
return hasHttpContext;
}
catch (NullReferenceException)
{
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment