diff --git a/src/Microsoft.AspNet.Routing/InlineRouteTemplateParser.cs b/src/Microsoft.AspNet.Routing/InlineRouteTemplateParser.cs index 6220202542..03903511b7 100644 --- a/src/Microsoft.AspNet.Routing/InlineRouteTemplateParser.cs +++ b/src/Microsoft.AspNet.Routing/InlineRouteTemplateParser.cs @@ -9,23 +9,23 @@ using Microsoft.AspNet.Routing.Template; namespace Microsoft.AspNet.Routing { - internal static class InlineRouteParameterParser + public static class InlineRouteParameterParser { // One or more characters, matches "id" - private const string ParameterNameRegex = @"(?.+?)"; + private const string ParameterNamePattern = @"(?.+?)"; // Zero or more inline constraints that start with a colon followed by zero or more characters // Optionally the constraint can have arguments within parentheses // - necessary to capture characters like ":" and "}" // Matches ":int", ":length(2)", ":regex(\})", ":regex(:)" zero or more times - private const string ConstraintRegex = @"(:(?.*?(\(.*?\))?))*"; + private const string ConstraintPattern = @"(:(?.*?(\(.*?\))?))*"; // A default value with an equal sign followed by zero or more characters // Matches "=", "=abc" - private const string DefaultValueRegex = @"(?(=.*?))?"; + private const string DefaultValueParameter = @"(?(=.*?))?"; private static readonly Regex _parameterRegex = new Regex( - "^" + ParameterNameRegex + ConstraintRegex + DefaultValueRegex + "$", + "^" + ParameterNamePattern + ConstraintPattern + DefaultValueParameter + "$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); public static TemplatePart ParseRouteParameter([NotNull] string routeParameter, [NotNull] IInlineConstraintResolver constraintResolver) @@ -37,7 +37,17 @@ namespace Microsoft.AspNet.Routing routeParameter = isOptional ? routeParameter.Substring(0, routeParameter.Length - 1) : routeParameter; var parameterMatch = _parameterRegex.Match(routeParameter); - string parameterName = parameterMatch.Groups["parameterName"].Value; + if (!parameterMatch.Success) + { + return TemplatePart.CreateParameter(name: string.Empty, + isCatchAll: isCatchAll, + isOptional: isOptional, + defaultValue: null, + inlineConstraint: null + ); + } + + var parameterName = parameterMatch.Groups["parameterName"].Value; // Add the default value if present var defaultValueGroup = parameterMatch.Groups["defaultValue"]; @@ -54,11 +64,11 @@ namespace Microsoft.AspNet.Routing inlineConstraint); } - private static object GetDefaultValue(Group defaultValueGroup) + private static string GetDefaultValue(Group defaultValueGroup) { if (defaultValueGroup.Success) { - string defaultValueMatch = defaultValueGroup.Value; + var defaultValueMatch = defaultValueGroup.Value; // Strip out the equal sign at the beginning Contract.Assert(defaultValueMatch.StartsWith("=", StringComparison.Ordinal)); @@ -74,13 +84,13 @@ namespace Microsoft.AspNet.Routing var parameterConstraints = new List(); foreach (Capture constraintCapture in constraintGroup.Captures) { - string inlineConstraint = constraintCapture.Value; + var inlineConstraint = constraintCapture.Value; var constraint = constraintResolver.ResolveConstraint(inlineConstraint); if (constraint == null) { throw new InvalidOperationException( - Resources.FormatInlineRouteParser_CouldNotResolveConstraint(constraintResolver.GetType().Name, - inlineConstraint)); + Resources.FormatInlineRouteParser_CouldNotResolveConstraint( + constraintResolver.GetType().Name, inlineConstraint)); } parameterConstraints.Add(constraint); diff --git a/src/Microsoft.AspNet.Routing/Template/TemplateParser.cs b/src/Microsoft.AspNet.Routing/Template/TemplateParser.cs index ab5f36252e..7e1981f3c7 100644 --- a/src/Microsoft.AspNet.Routing/Template/TemplateParser.cs +++ b/src/Microsoft.AspNet.Routing/Template/TemplateParser.cs @@ -173,7 +173,7 @@ namespace Microsoft.AspNet.Routing.Template } var rawParameter = context.Capture(); - + // At this point, we need to parse the raw name for inline constraint, // default values and optional parameters. var templatePart = InlineRouteParameterParser.ParseRouteParameter(rawParameter, diff --git a/src/Microsoft.AspNet.Routing/Template/TemplateRoute.cs b/src/Microsoft.AspNet.Routing/Template/TemplateRoute.cs index 30687c2024..c86a880689 100644 --- a/src/Microsoft.AspNet.Routing/Template/TemplateRoute.cs +++ b/src/Microsoft.AspNet.Routing/Template/TemplateRoute.cs @@ -191,6 +191,7 @@ namespace Microsoft.AspNet.Routing.Template { _constraints[parameter.Name] = parameter.InlineConstraint; } + } } if (parameter.DefaultValue != null)