Adding DataTokens Support.
This commit is contained in:
parent
860fcf65f3
commit
efdd3054d2
|
|
@ -31,6 +31,16 @@ namespace Microsoft.AspNet.Routing
|
||||||
string template,
|
string template,
|
||||||
object defaults,
|
object defaults,
|
||||||
object constraints)
|
object constraints)
|
||||||
|
{
|
||||||
|
return MapRoute(routeCollectionBuilder, name, template, defaults, constraints, dataTokens: null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IRouteBuilder MapRoute(this IRouteBuilder routeCollectionBuilder,
|
||||||
|
string name,
|
||||||
|
string template,
|
||||||
|
object defaults,
|
||||||
|
object constraints,
|
||||||
|
object dataTokens)
|
||||||
{
|
{
|
||||||
if (routeCollectionBuilder.DefaultHandler == null)
|
if (routeCollectionBuilder.DefaultHandler == null)
|
||||||
{
|
{
|
||||||
|
|
@ -45,7 +55,9 @@ namespace Microsoft.AspNet.Routing
|
||||||
template,
|
template,
|
||||||
ObjectToDictionary(defaults),
|
ObjectToDictionary(defaults),
|
||||||
ObjectToDictionary(constraints),
|
ObjectToDictionary(constraints),
|
||||||
|
ObjectToDictionary(dataTokens),
|
||||||
inlineConstraintResolver));
|
inlineConstraintResolver));
|
||||||
|
|
||||||
return routeCollectionBuilder;
|
return routeCollectionBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,5 +18,7 @@ namespace Microsoft.AspNet.Routing
|
||||||
public List<IRouter> Routers { get; private set; }
|
public List<IRouter> Routers { get; private set; }
|
||||||
|
|
||||||
public IDictionary<string, object> Values { get; set; }
|
public IDictionary<string, object> Values { get; set; }
|
||||||
|
|
||||||
|
public IDictionary<string, object> DataTokens { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -15,6 +15,7 @@ namespace Microsoft.AspNet.Routing.Template
|
||||||
{
|
{
|
||||||
private readonly IDictionary<string, object> _defaults;
|
private readonly IDictionary<string, object> _defaults;
|
||||||
private readonly IDictionary<string, IRouteConstraint> _constraints;
|
private readonly IDictionary<string, IRouteConstraint> _constraints;
|
||||||
|
private readonly IDictionary<string, object> _dataTokens;
|
||||||
private readonly IRouter _target;
|
private readonly IRouter _target;
|
||||||
private readonly RouteTemplate _parsedTemplate;
|
private readonly RouteTemplate _parsedTemplate;
|
||||||
private readonly string _routeTemplate;
|
private readonly string _routeTemplate;
|
||||||
|
|
@ -24,7 +25,12 @@ namespace Microsoft.AspNet.Routing.Template
|
||||||
private ILogger _constraintLogger;
|
private ILogger _constraintLogger;
|
||||||
|
|
||||||
public TemplateRoute(IRouter target, string routeTemplate, IInlineConstraintResolver inlineConstraintResolver)
|
public TemplateRoute(IRouter target, string routeTemplate, IInlineConstraintResolver inlineConstraintResolver)
|
||||||
: this(target, routeTemplate, null, null, inlineConstraintResolver)
|
: this(target,
|
||||||
|
routeTemplate,
|
||||||
|
defaults: null,
|
||||||
|
constraints: null,
|
||||||
|
dataTokens: null,
|
||||||
|
inlineConstraintResolver: inlineConstraintResolver)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,8 +38,9 @@ namespace Microsoft.AspNet.Routing.Template
|
||||||
string routeTemplate,
|
string routeTemplate,
|
||||||
IDictionary<string, object> defaults,
|
IDictionary<string, object> defaults,
|
||||||
IDictionary<string, object> constraints,
|
IDictionary<string, object> constraints,
|
||||||
|
IDictionary<string, object> dataTokens,
|
||||||
IInlineConstraintResolver inlineConstraintResolver)
|
IInlineConstraintResolver inlineConstraintResolver)
|
||||||
: this(target, null, routeTemplate, defaults, constraints, inlineConstraintResolver)
|
: this(target, null, routeTemplate, defaults, constraints, dataTokens, inlineConstraintResolver)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,6 +49,7 @@ namespace Microsoft.AspNet.Routing.Template
|
||||||
string routeTemplate,
|
string routeTemplate,
|
||||||
IDictionary<string, object> defaults,
|
IDictionary<string, object> defaults,
|
||||||
IDictionary<string, object> constraints,
|
IDictionary<string, object> constraints,
|
||||||
|
IDictionary<string, object> dataTokens,
|
||||||
IInlineConstraintResolver inlineConstraintResolver)
|
IInlineConstraintResolver inlineConstraintResolver)
|
||||||
{
|
{
|
||||||
_target = target;
|
_target = target;
|
||||||
|
|
@ -50,6 +58,7 @@ namespace Microsoft.AspNet.Routing.Template
|
||||||
_defaults = defaults ?? new RouteValueDictionary();
|
_defaults = defaults ?? new RouteValueDictionary();
|
||||||
_constraints = RouteConstraintBuilder.BuildConstraints(constraints, _routeTemplate) ??
|
_constraints = RouteConstraintBuilder.BuildConstraints(constraints, _routeTemplate) ??
|
||||||
new Dictionary<string, IRouteConstraint>();
|
new Dictionary<string, IRouteConstraint>();
|
||||||
|
_dataTokens = dataTokens ?? new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
// The parser will throw for invalid routes.
|
// The parser will throw for invalid routes.
|
||||||
_parsedTemplate = TemplateParser.Parse(RouteTemplate, inlineConstraintResolver);
|
_parsedTemplate = TemplateParser.Parse(RouteTemplate, inlineConstraintResolver);
|
||||||
|
|
@ -66,6 +75,11 @@ namespace Microsoft.AspNet.Routing.Template
|
||||||
get { return _defaults; }
|
get { return _defaults; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IDictionary<string, object> DataTokens
|
||||||
|
{
|
||||||
|
get { return _dataTokens; }
|
||||||
|
}
|
||||||
|
|
||||||
public string RouteTemplate
|
public string RouteTemplate
|
||||||
{
|
{
|
||||||
get { return _routeTemplate; }
|
get { return _routeTemplate; }
|
||||||
|
|
@ -117,6 +131,7 @@ namespace Microsoft.AspNet.Routing.Template
|
||||||
RouteDirection.IncomingRequest,
|
RouteDirection.IncomingRequest,
|
||||||
_constraintLogger))
|
_constraintLogger))
|
||||||
{
|
{
|
||||||
|
context.RouteData.DataTokens = _dataTokens;
|
||||||
await _target.RouteAsync(context);
|
await _target.RouteAsync(context);
|
||||||
|
|
||||||
if (_logger.IsEnabled(TraceType.Information))
|
if (_logger.IsEnabled(TraceType.Information))
|
||||||
|
|
|
||||||
|
|
@ -662,7 +662,61 @@ namespace Microsoft.AspNet.Routing.Template
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Route Registration
|
#region Route Registration
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> DataTokens
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new object[] {
|
||||||
|
new Dictionary<string, object> { { "key1", "data1" }, { "key2", 13 } },
|
||||||
|
new Dictionary<string, object> { { "key1", "data1" }, { "key2", 13 } },
|
||||||
|
};
|
||||||
|
yield return new object[] {
|
||||||
|
new RouteValueDictionary { { "key1", "data1" }, { "key2", 13 } },
|
||||||
|
new Dictionary<string, object> { { "key1", "data1" }, { "key2", 13 } },
|
||||||
|
};
|
||||||
|
yield return new object[] {
|
||||||
|
new object(),
|
||||||
|
new Dictionary<string,object>(),
|
||||||
|
};
|
||||||
|
yield return new object[] {
|
||||||
|
null,
|
||||||
|
new Dictionary<string, object>()
|
||||||
|
};
|
||||||
|
yield return new object[] {
|
||||||
|
new { key1 = "data1", key2 = 13 },
|
||||||
|
new Dictionary<string, object> { { "key1", "data1" }, { "key2", 13 } },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData("DataTokens")]
|
||||||
|
public void RegisteringRoute_WithDataTokens_AbleToAddTheRoute(object dataToken,
|
||||||
|
IDictionary<string, object> expectedDictionary)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var routeBuilder = CreateRouteBuilder();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
routeBuilder.MapRoute("mockName",
|
||||||
|
"{controller}/{action}",
|
||||||
|
defaults: null,
|
||||||
|
constraints: null,
|
||||||
|
dataTokens: dataToken);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var templateRoute = (TemplateRoute)routeBuilder.Routes[0];
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(expectedDictionary.Count, templateRoute.DataTokens.Count);
|
||||||
|
foreach (var expectedKey in expectedDictionary.Keys)
|
||||||
|
{
|
||||||
|
Assert.True(templateRoute.DataTokens.ContainsKey(expectedKey));
|
||||||
|
Assert.Equal(expectedDictionary[expectedKey], templateRoute.DataTokens[expectedKey]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void RegisteringRouteWithInvalidConstraints_Throws()
|
public void RegisteringRouteWithInvalidConstraints_Throws()
|
||||||
|
|
@ -794,12 +848,19 @@ namespace Microsoft.AspNet.Routing.Template
|
||||||
return new TemplateRoute(CreateTarget(accept), template, _inlineConstraintResolver);
|
return new TemplateRoute(CreateTarget(accept), template, _inlineConstraintResolver);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TemplateRoute CreateRoute(string template, object defaults, bool accept = true, object constraints = null)
|
private static TemplateRoute CreateRoute(string template,
|
||||||
|
object defaults,
|
||||||
|
bool accept = true,
|
||||||
|
object constraints = null,
|
||||||
|
object dataTokens = null)
|
||||||
{
|
{
|
||||||
return new TemplateRoute(CreateTarget(accept),
|
return new TemplateRoute(CreateTarget(accept),
|
||||||
template,
|
template,
|
||||||
new RouteValueDictionary(defaults),
|
new RouteValueDictionary(defaults),
|
||||||
(constraints as IDictionary<string, object>) ?? new RouteValueDictionary(constraints),
|
(constraints as IDictionary<string, object>) ??
|
||||||
|
new RouteValueDictionary(constraints),
|
||||||
|
(dataTokens as IDictionary<string, object>) ??
|
||||||
|
new RouteValueDictionary(constraints),
|
||||||
_inlineConstraintResolver);
|
_inlineConstraintResolver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -809,6 +870,7 @@ namespace Microsoft.AspNet.Routing.Template
|
||||||
template,
|
template,
|
||||||
new RouteValueDictionary(),
|
new RouteValueDictionary(),
|
||||||
constraints: null,
|
constraints: null,
|
||||||
|
dataTokens: null,
|
||||||
inlineConstraintResolver: _inlineConstraintResolver);
|
inlineConstraintResolver: _inlineConstraintResolver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -818,6 +880,7 @@ namespace Microsoft.AspNet.Routing.Template
|
||||||
template,
|
template,
|
||||||
new RouteValueDictionary(defaults),
|
new RouteValueDictionary(defaults),
|
||||||
constraints: null,
|
constraints: null,
|
||||||
|
dataTokens: null,
|
||||||
inlineConstraintResolver: _inlineConstraintResolver);
|
inlineConstraintResolver: _inlineConstraintResolver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue