Created
August 2, 2017 06:28
-
-
Save agc93/473fa9441cfde4276b7ec658edb97437 to your computer and use it in GitHub Desktop.
Runtime route prefixing
This file contains 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
[Route("{prefix?}")] | |
public class ApiController : Controller {} |
This file contains 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 class RouteActionConstraint : IActionConstraint, IActionConstraintMetadata | |
{ | |
private readonly ILogger _logger; | |
public string Prefix { get; private set; } | |
internal RouteActionConstraint(string prefix, ILogger logger) { | |
Prefix = prefix; | |
_logger = logger; | |
} | |
public int Order => 0; | |
public bool Accept(ActionConstraintContext context) | |
{ | |
_logger.LogDebug("Matching route using prefix: {0}", Prefix ?? string.Empty); | |
return string.IsNullOrWhiteSpace(Prefix) | |
? true | |
: context.RouteContext.RouteData.Values.First().Value?.ToString() == Prefix; | |
// this is specifically for prefix, and would need to be improved for other routing scenarious | |
} | |
} |
This file contains 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 class PrefixRouteConvention : IActionModelConvention | |
{ | |
private readonly string _prefix; | |
private readonly ILoggerFactory _factory; | |
public DownlinkRouteConvention(IConfiguration config, ILoggerFactory factory) { | |
_prefix = config.GetValue("RoutePrefix", string.Empty); | |
_factory = factory; | |
} | |
public void Apply(ActionModel action) | |
{ | |
if (action.Controller.ControllerType == typeof(Controllers.ApiController)) { // restrict to this controller | |
foreach(var selector in action.Selectors) { | |
selector.ActionConstraints.Add( | |
new RouteActionConstraint( | |
_prefix, | |
_factory.CreateLogger(nameof(RouteActionConstraint)))); | |
} | |
} | |
} | |
} |
This file contains 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
services.AddSingleton<PrefixRouteConvention>(); | |
services.Configure<MvcOptions>(opts => opts.Conventions.Add(services.BuildServiceProvider().GetService<PrefixRouteConvention>())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment