Created
March 14, 2012 16:01
-
-
Save SLaks/2037478 to your computer and use it in GitHub Desktop.
Evil Razor scoping trick
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 delegate IDisposable ViewScope(); | |
public static class Scope { | |
public static readonly ViewScope StateOfEvil = new object().EnterScope; | |
private static IDisposable EnterScope(this object id) { | |
HttpContext.Current.Items.Add(id, null); | |
return new Disposable(() => HttpContext.Current.Items.Remove(id)); | |
} | |
} | |
public static class ScopeExtensions { | |
public static bool IsActive(this ViewScope scope) { | |
return HttpContext.Current.Items.Contains(scope.Target); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: