Skip to content

Instantly share code, notes, and snippets.

@sergeyt
Created October 3, 2014 03:06
Show Gist options
  • Save sergeyt/b0d4d3c7398b446c41ad to your computer and use it in GitHub Desktop.
Save sergeyt/b0d4d3c7398b446c41ad to your computer and use it in GitHub Desktop.
Mini asp.net router
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