diff --git a/samples/RoutingSample/PrefixRoute.cs b/samples/RoutingSample/PrefixRoute.cs index 7b89fcf975..833add299a 100644 --- a/samples/RoutingSample/PrefixRoute.cs +++ b/samples/RoutingSample/PrefixRoute.cs @@ -8,12 +8,12 @@ namespace RoutingSample { internal class PrefixRoute : IRouter { - private readonly IRouter _next; + private readonly IRouter _target; private readonly string _prefix; - public PrefixRoute(IRouter next, string prefix) + public PrefixRoute(IRouter target, string prefix) { - _next = next; + _target = target; if (prefix == null) { @@ -46,7 +46,7 @@ namespace RoutingSample } } - await _next.RouteAsync(context); + await _target.RouteAsync(context); } } diff --git a/samples/RoutingSample/RouteCollectionExtensions.cs b/samples/RoutingSample/RouteCollectionExtensions.cs index 68b07774dd..ac557efd86 100644 --- a/samples/RoutingSample/RouteCollectionExtensions.cs +++ b/samples/RoutingSample/RouteCollectionExtensions.cs @@ -1,5 +1,6 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +using System; using Microsoft.AspNet.Routing; namespace RoutingSample @@ -8,6 +9,11 @@ namespace RoutingSample { public static IRouteCollection AddPrefixRoute(this IRouteCollection routes, string prefix) { + if (routes.DefaultHandler == null) + { + throw new InvalidOperationException("DefaultHandler must be set."); + } + return AddPrefixRoute(routes, prefix, routes.DefaultHandler); } diff --git a/samples/RoutingSample/Startup.cs b/samples/RoutingSample/Startup.cs index 238390715a..f4d39a1a2e 100644 --- a/samples/RoutingSample/Startup.cs +++ b/samples/RoutingSample/Startup.cs @@ -3,8 +3,6 @@ #if NET45 using Microsoft.AspNet.Abstractions; -using Microsoft.AspNet.Routing; -using Microsoft.AspNet.Routing.Owin; using Microsoft.AspNet.Routing.Template; using Owin; @@ -28,7 +26,7 @@ namespace RoutingSample routes.DefaultHandler = endpoint1; routes.AddPrefixRoute("api/store"); - routes.AddTemplateRoute("api/{controller}/{*extra}", new { controller = "Store" }); + routes.MapRoute("api/{controller}/{*extra}", new { controller = "Store" }); routes.AddPrefixRoute("hello/world", endpoint2); routes.AddPrefixRoute("", endpoint2); diff --git a/src/Microsoft.AspNet.Routing/BindPathContext.cs b/src/Microsoft.AspNet.Routing/BindPathContext.cs index c2101fd4bf..a69086b9f9 100644 --- a/src/Microsoft.AspNet.Routing/BindPathContext.cs +++ b/src/Microsoft.AspNet.Routing/BindPathContext.cs @@ -20,7 +20,7 @@ namespace Microsoft.AspNet.Routing public bool IsBound { get; set; } - public string Path { get; set; } + public string BoundPath { get; set; } public IDictionary Values { get; private set; } } diff --git a/src/Microsoft.AspNet.Routing/Owin/BuilderExtensions.cs b/src/Microsoft.AspNet.Routing/BuilderExtensions.cs similarity index 88% rename from src/Microsoft.AspNet.Routing/Owin/BuilderExtensions.cs rename to src/Microsoft.AspNet.Routing/BuilderExtensions.cs index 8bb5c67e0a..7abc325f3c 100644 --- a/src/Microsoft.AspNet.Routing/Owin/BuilderExtensions.cs +++ b/src/Microsoft.AspNet.Routing/BuilderExtensions.cs @@ -1,8 +1,8 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. -using Microsoft.AspNet.Abstractions; +using Microsoft.AspNet.Routing; -namespace Microsoft.AspNet.Routing.Owin +namespace Microsoft.AspNet.Abstractions { public static class BuilderExtensions { diff --git a/src/Microsoft.AspNet.Routing/Owin/RouterMiddleware.cs b/src/Microsoft.AspNet.Routing/RouterMiddleware.cs similarity index 89% rename from src/Microsoft.AspNet.Routing/Owin/RouterMiddleware.cs rename to src/Microsoft.AspNet.Routing/RouterMiddleware.cs index b45578742f..ed982cad58 100644 --- a/src/Microsoft.AspNet.Routing/Owin/RouterMiddleware.cs +++ b/src/Microsoft.AspNet.Routing/RouterMiddleware.cs @@ -1,10 +1,9 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. -using System.Net; using System.Threading.Tasks; -using Microsoft.AspNet.Abstractions; +using Microsoft.AspNet.Routing; -namespace Microsoft.AspNet.Routing.Owin +namespace Microsoft.AspNet.Abstractions { public class RouterMiddleware { diff --git a/src/Microsoft.AspNet.Routing/Template/RouteCollectionExtensions.cs b/src/Microsoft.AspNet.Routing/Template/RouteCollectionExtensions.cs index e33ba3a0cc..80b04c97ec 100644 --- a/src/Microsoft.AspNet.Routing/Template/RouteCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Routing/Template/RouteCollectionExtensions.cs @@ -1,25 +1,31 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +using System; using System.Collections.Generic; namespace Microsoft.AspNet.Routing.Template { public static class RouteCollectionExtensions { - public static IRouteCollection AddTemplateRoute(this IRouteCollection routes, string template) + public static IRouteCollection MapRoute(this IRouteCollection routes, string template) { - AddTemplateRoute(routes, template, null); + MapRoute(routes, template, null); return routes; } - public static IRouteCollection AddTemplateRoute(this IRouteCollection routes, string template, object defaults) + public static IRouteCollection MapRoute(this IRouteCollection routes, string template, object defaults) { - AddTemplateRoute(routes, template, new RouteValueDictionary(defaults)); + MapRoute(routes, template, new RouteValueDictionary(defaults)); return routes; } - public static IRouteCollection AddTemplateRoute(this IRouteCollection routes, string template, IDictionary defaults) + public static IRouteCollection MapRoute(this IRouteCollection routes, string template, IDictionary defaults) { + if (routes.DefaultHandler == null) + { + throw new InvalidOperationException("DefaultHandler must be set."); + } + routes.Add(new TemplateRoute(routes.DefaultHandler, template, defaults)); return routes; } diff --git a/src/Microsoft.AspNet.Routing/Template/TemplateRoute.cs b/src/Microsoft.AspNet.Routing/Template/TemplateRoute.cs index 0d6673eeb6..e980f58ccd 100644 --- a/src/Microsoft.AspNet.Routing/Template/TemplateRoute.cs +++ b/src/Microsoft.AspNet.Routing/Template/TemplateRoute.cs @@ -10,25 +10,25 @@ namespace Microsoft.AspNet.Routing.Template public class TemplateRoute : IRouter { private readonly IDictionary _defaults; - private readonly IRouter _next; + private readonly IRouter _target; private readonly Template _parsedTemplate; private readonly string _routeTemplate; private readonly TemplateMatcher _matcher; private readonly TemplateBinder _binder; - public TemplateRoute(IRouter next, string routeTemplate) - : this(next, routeTemplate, null) + public TemplateRoute(IRouter target, string routeTemplate) + : this(target, routeTemplate, null) { } - public TemplateRoute(IRouter next, string routeTemplate, IDictionary defaults) + public TemplateRoute(IRouter target, string routeTemplate, IDictionary defaults) { - if (next == null) + if (target == null) { - throw new ArgumentNullException("next"); + throw new ArgumentNullException("target"); } - _next = next; + _target = target; _routeTemplate = routeTemplate ?? string.Empty; _defaults = defaults ?? new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -70,7 +70,7 @@ namespace Microsoft.AspNet.Routing.Template } else { - await _next.RouteAsync(new RouteContext(context.HttpContext){ Values = values }); + await _target.RouteAsync(new RouteContext(context.HttpContext){ Values = values }); } } @@ -83,11 +83,13 @@ namespace Microsoft.AspNet.Routing.Template { return; } - - _next.BindPath(context); - if (context.IsBound && context.Path == null) + + context.BoundPath = path; + _target.BindPath(context); + + if (!context.IsBound) { - context.Path = path; + context.BoundPath = null; } } } diff --git a/test/Microsoft.AspNet.Routing.Tests/Template/TemplateRouteTests.cs b/test/Microsoft.AspNet.Routing.Tests/Template/TemplateRouteTests.cs index f6b6e5489c..2baaaf3a69 100644 --- a/test/Microsoft.AspNet.Routing.Tests/Template/TemplateRouteTests.cs +++ b/test/Microsoft.AspNet.Routing.Tests/Template/TemplateRouteTests.cs @@ -117,7 +117,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests // Assert Assert.True(context.IsBound); - Assert.Equal("Home", context.Path); + Assert.Equal("Home", context.BoundPath); } [Fact] @@ -132,7 +132,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests // Assert Assert.False(context.IsBound); - Assert.Null(context.Path); + Assert.Null(context.BoundPath); } [Fact] @@ -147,7 +147,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests // Assert Assert.False(context.IsBound); - Assert.Null(context.Path); + Assert.Null(context.BoundPath); } [Fact] @@ -162,7 +162,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests // Assert Assert.True(context.IsBound); - Assert.Equal("Home/Index", context.Path); + Assert.Equal("Home/Index", context.BoundPath); } private static BindPathContext CreateRouteBindContext(object values) @@ -186,26 +186,26 @@ namespace Microsoft.AspNet.Routing.Template.Tests private static TemplateRoute CreateRoute(string template, bool accept = true) { - return new TemplateRoute(CreateEndpoint(accept), template); + return new TemplateRoute(CreateTarget(accept), template); } private static TemplateRoute CreateRoute(string template, object defaults, bool accept = true) { - return new TemplateRoute(CreateEndpoint(accept), template, new RouteValueDictionary(defaults)); + return new TemplateRoute(CreateTarget(accept), template, new RouteValueDictionary(defaults)); } - private static IRouter CreateEndpoint(bool accept = true) + private static IRouter CreateTarget(bool accept = true) { - var endpoint = new Mock(MockBehavior.Strict); - endpoint + var target = new Mock(MockBehavior.Strict); + target .Setup(e => e.BindPath(It.IsAny())) .Callback(c => c.IsBound = accept); - endpoint + target .Setup(e => e.RouteAsync(It.IsAny())) .Callback(async (c) => c.IsHandled = accept); - return endpoint.Object; + return target.Object; } } }