Custom RouteContraint with IOptions

114 Views Asked by At

Is there a way to pass IOptions into a custom IRouteConstraint? I'm trying to check an appsetting to see if I need to have a certain route or not.

1

There are 1 best solutions below

4
On BEST ANSWER

Can't find any references in the moment, but

  1. either it works when you pass it in the constructor (I assume you tried it and it didn't work)
  2. get it from the HttpContext, which is passed to the Match method

Example:

public class MyConstraint : IRouteConstraint
{
    public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
    {
        var options = httpContext.RequestServices.GetService<IOptions<MySettings>>();

        return ...;
    }
}