Skip to content

Instantly share code, notes, and snippets.

@SLaks
Created March 14, 2012 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SLaks/2037478 to your computer and use it in GitHub Desktop.
Save SLaks/2037478 to your computer and use it in GitHub Desktop.
Evil Razor scoping trick
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);
}
}
@SLaks
Copy link
Author

SLaks commented Mar 14, 2012

Usage:

@using (Scope.StateOfEvil()) {
    @: Active? @Scope.StateOfEvil.IsActive()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment