// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Routing.Internal;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Routing
{
///
/// The default implementation of . Resolves constraints by parsing
/// a constraint key and constraint arguments, using a map to resolve the constraint type, and calling an
/// appropriate constructor for the constraint type.
///
public class DefaultInlineConstraintResolver : IInlineConstraintResolver
{
private readonly IDictionary _inlineConstraintMap;
private readonly IServiceProvider _serviceProvider;
///
/// Initializes a new instance of the class.
///
///
/// Accessor for containing the constraints of interest.
///
[Obsolete("This constructor is obsolete. Use DefaultInlineConstraintResolver.ctor(IOptions, IServiceProvider) instead.")]
public DefaultInlineConstraintResolver(IOptions routeOptions)
{
_inlineConstraintMap = routeOptions.Value.ConstraintMap;
}
public DefaultInlineConstraintResolver(IOptions routeOptions, IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}
_inlineConstraintMap = routeOptions.Value.ConstraintMap;
_serviceProvider = serviceProvider;
}
///
///
/// A typical constraint looks like the following
/// "exampleConstraint(arg1, arg2, 12)".
/// Here if the type registered for exampleConstraint has a single constructor with one argument,
/// The entire string "arg1, arg2, 12" will be treated as a single argument.
/// In all other cases arguments are split at comma.
///
public virtual IRouteConstraint ResolveConstraint(string inlineConstraint)
{
if (inlineConstraint == null)
{
throw new ArgumentNullException(nameof(inlineConstraint));
}
// This will return null if the text resolves to a non-IRouteConstraint
return ParameterPolicyActivator.ResolveParameterPolicy(
_inlineConstraintMap,
_serviceProvider,
inlineConstraint,
out _);
}
}
}