Created
October 3, 2014 03:06
-
-
Save sergeyt/b0d4d3c7398b446c41ad to your computer and use it in GitHub Desktop.
Mini asp.net router
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
private sealed class Router | |
{ | |
private readonly HttpContext _context; | |
private bool? _lastOp; | |
private bool _cumulativeResult; | |
public Router(HttpContext context) | |
{ | |
if (context == null) throw new ArgumentNullException("context"); | |
_context = context; | |
} | |
public Router Get(Action action) | |
{ | |
if ((_lastOp == null || _lastOp.Value) && "GET".Equals(_context.Request.HttpMethod)) | |
action(); | |
return this; | |
} | |
public Router Post(Action action) | |
{ | |
if ((_lastOp == null || _lastOp.Value) && "POST".Equals(_context.Request.HttpMethod)) | |
action(); | |
return this; | |
} | |
public Router All(Action action) | |
{ | |
if (_lastOp == null || _lastOp.Value) | |
action(); | |
return this; | |
} | |
public Router Route(string path) | |
{ | |
var vpath = _context.Request.Path; | |
_lastOp = vpath.Equals(path, StringComparison.InvariantCultureIgnoreCase); | |
_cumulativeResult = _cumulativeResult || _lastOp.Value; | |
return this; | |
} | |
public Router Stub(Action action) | |
{ | |
if (!_cumulativeResult) | |
{ | |
action(); | |
} | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment