Created
May 25, 2012 15:26
-
-
Save jarrettmeyer/2788772 to your computer and use it in GitHub Desktop.
Determine if there is a current HTTP context without referencing System.Web
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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