From c796188231edb9cef7178cc6ee52e742e3231bdd Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Mon, 3 Feb 2014 15:56:44 -0800 Subject: [PATCH] integrating abstractions with routing --- samples/RoutingSample/AppBuilderExtensions.cs | 18 +++++++++++++++++ samples/RoutingSample/OwinRouteEndpoint.cs | 7 +++++-- samples/RoutingSample/Startup.cs | 5 +++++ samples/RoutingSample/project.json | 17 ++++++++++------ .../DefaultRouteEngine.cs | 9 +++++---- .../IRouteEndpoint.cs | 4 ++-- src/Microsoft.AspNet.Routing/IRouteEngine.cs | 4 ++-- src/Microsoft.AspNet.Routing/IRouteValues.cs | 14 +++++++++++++ ...lderExtensions.cs => BuilderExtensions.cs} | 11 ++++------ .../Owin/RouterMiddleware.cs | 13 ++++-------- src/Microsoft.AspNet.Routing/RouteContext.cs | 8 ++++---- src/Microsoft.AspNet.Routing/RouteValues.cs | 20 +++++++++++++++++++ src/Microsoft.AspNet.Routing/project.json | 7 ++----- 13 files changed, 96 insertions(+), 41 deletions(-) create mode 100644 samples/RoutingSample/AppBuilderExtensions.cs create mode 100644 src/Microsoft.AspNet.Routing/IRouteValues.cs rename src/Microsoft.AspNet.Routing/Owin/{AppBuilderExtensions.cs => BuilderExtensions.cs} (61%) create mode 100644 src/Microsoft.AspNet.Routing/RouteValues.cs diff --git a/samples/RoutingSample/AppBuilderExtensions.cs b/samples/RoutingSample/AppBuilderExtensions.cs new file mode 100644 index 0000000000..bd27fdde8f --- /dev/null +++ b/samples/RoutingSample/AppBuilderExtensions.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. + +using Microsoft.AspNet.Routing; +using Microsoft.AspNet.Routing.Owin; +using Owin; + +namespace RoutingSample +{ + public static class AppBuilderExtensions + { + public static IRouteCollection UseRouter(this IAppBuilder app) + { + IRouteCollection routes = null; + app.UseBuilder((b) => routes = b.UseRouter()); + return routes; + } + } +} diff --git a/samples/RoutingSample/OwinRouteEndpoint.cs b/samples/RoutingSample/OwinRouteEndpoint.cs index f903bfee2f..734a81b9da 100644 --- a/samples/RoutingSample/OwinRouteEndpoint.cs +++ b/samples/RoutingSample/OwinRouteEndpoint.cs @@ -3,6 +3,8 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using Microsoft.AspNet.Abstractions; +using Microsoft.AspNet.PipelineCore.Owin; using Microsoft.AspNet.Routing; namespace RoutingSample @@ -16,9 +18,10 @@ namespace RoutingSample _appFunc = appFunc; } - public Task Invoke(IDictionary context) + public Task Invoke(HttpContext context) { - return _appFunc(context); + var owinContext = context.GetFeature().Environment; + return _appFunc(owinContext); } } } diff --git a/samples/RoutingSample/Startup.cs b/samples/RoutingSample/Startup.cs index 2f6f4d2d1a..610d56f619 100644 --- a/samples/RoutingSample/Startup.cs +++ b/samples/RoutingSample/Startup.cs @@ -6,6 +6,10 @@ using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.Routing.Owin; using Owin; +using Microsoft.AspNet.PipelineCore.Owin; +using Microsoft.AspNet.Routing; +using System; +using Microsoft.AspNet.Abstractions; namespace RoutingSample { @@ -14,6 +18,7 @@ namespace RoutingSample public void Configuration(IAppBuilder appBuilder) { var routes = appBuilder.UseRouter(); + OwinRouteEndpoint endpoint1 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "match1")); OwinRouteEndpoint endpoint2 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "Hello, World!")); diff --git a/samples/RoutingSample/project.json b/samples/RoutingSample/project.json index 71c3fc2226..35f33aab61 100644 --- a/samples/RoutingSample/project.json +++ b/samples/RoutingSample/project.json @@ -1,16 +1,21 @@ { "version": "0.1-alpha-*", "dependencies": { + "Microsoft.AspNet.Abstractions" : "0.1-alpha-*", + "Microsoft.AspNet.AppBuilderSupport": "0.1-alpha-*", + "Microsoft.AspNet.FeatureModel": "0.1-alpha-*", + "Microsoft.AspNet.HttpFeature": "0.1-alpha-*", + "Microsoft.AspNet.PipelineCore": "0.1-alpha-*", "Microsoft.AspNet.Routing" : "" }, "configurations": { "net45": { - "dependencies": { - "Owin": "1.0", - "Microsoft.Owin" : "2.1.0", - "Microsoft.Owin.Host.HttpListener" : "2.1.0", - "Microsoft.Owin.Hosting" : "2.1.0" - } + "dependencies": { + "Owin": "1.0", + "Microsoft.Owin" : "2.1.0", + "Microsoft.Owin.Host.HttpListener" : "2.1.0", + "Microsoft.Owin.Hosting" : "2.1.0" + } } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Routing/DefaultRouteEngine.cs b/src/Microsoft.AspNet.Routing/DefaultRouteEngine.cs index c4a56b458b..07c4a3f517 100644 --- a/src/Microsoft.AspNet.Routing/DefaultRouteEngine.cs +++ b/src/Microsoft.AspNet.Routing/DefaultRouteEngine.cs @@ -1,12 +1,11 @@ // 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; using System.Threading.Tasks; +using Microsoft.AspNet.Abstractions; namespace Microsoft.AspNet.Routing { - internal class DefaultRouteEngine : IRouteEngine + public class DefaultRouteEngine : IRouteEngine { public DefaultRouteEngine(IRouteCollection routes) { @@ -19,7 +18,7 @@ namespace Microsoft.AspNet.Routing private set; } - public async Task Invoke(IDictionary context) + public async Task Invoke(HttpContext context) { RouteContext routeContext = new RouteContext(context); @@ -30,6 +29,8 @@ namespace Microsoft.AspNet.Routing RouteMatch match = route.Match(routeContext); if (match != null) { + context.SetFeature(new RouteValues(match.Values)); + await match.Endpoint.Invoke(context); return true; } diff --git a/src/Microsoft.AspNet.Routing/IRouteEndpoint.cs b/src/Microsoft.AspNet.Routing/IRouteEndpoint.cs index badb71f73b..b5ebcc284d 100644 --- a/src/Microsoft.AspNet.Routing/IRouteEndpoint.cs +++ b/src/Microsoft.AspNet.Routing/IRouteEndpoint.cs @@ -1,12 +1,12 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. -using System.Collections.Generic; using System.Threading.Tasks; +using Microsoft.AspNet.Abstractions; namespace Microsoft.AspNet.Routing { public interface IRouteEndpoint { - Task Invoke(IDictionary context); + Task Invoke(HttpContext context); } } diff --git a/src/Microsoft.AspNet.Routing/IRouteEngine.cs b/src/Microsoft.AspNet.Routing/IRouteEngine.cs index e813f79b42..d54c7dc581 100644 --- a/src/Microsoft.AspNet.Routing/IRouteEngine.cs +++ b/src/Microsoft.AspNet.Routing/IRouteEngine.cs @@ -1,12 +1,12 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. -using System.Collections.Generic; using System.Threading.Tasks; +using Microsoft.AspNet.Abstractions; namespace Microsoft.AspNet.Routing { public interface IRouteEngine { - Task Invoke(IDictionary context); + Task Invoke(HttpContext context); } } diff --git a/src/Microsoft.AspNet.Routing/IRouteValues.cs b/src/Microsoft.AspNet.Routing/IRouteValues.cs new file mode 100644 index 0000000000..7e44c9fc86 --- /dev/null +++ b/src/Microsoft.AspNet.Routing/IRouteValues.cs @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. + +using System.Collections.Generic; + +namespace Microsoft.AspNet.Routing +{ + public interface IRouteValues + { + IDictionary Values + { + get; + } + } +} diff --git a/src/Microsoft.AspNet.Routing/Owin/AppBuilderExtensions.cs b/src/Microsoft.AspNet.Routing/Owin/BuilderExtensions.cs similarity index 61% rename from src/Microsoft.AspNet.Routing/Owin/AppBuilderExtensions.cs rename to src/Microsoft.AspNet.Routing/Owin/BuilderExtensions.cs index 06e70a5ca1..999517009b 100644 --- a/src/Microsoft.AspNet.Routing/Owin/AppBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Routing/Owin/BuilderExtensions.cs @@ -1,23 +1,20 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. -#if NET45 - -using Owin; +using Microsoft.AspNet.Abstractions; namespace Microsoft.AspNet.Routing.Owin { - public static class AppBuilderExtensions + public static class BuilderExtensions { - public static IRouteCollection UseRouter(this IAppBuilder builder) + public static IRouteCollection UseRouter(this IBuilder builder) { var routes = new DefaultRouteCollection(); var engine = new DefaultRouteEngine(routes); - builder.Use(typeof(RouterMiddleware), engine); + builder.Use((next) => new RouterMiddleware(next, engine).Invoke); return routes; } } } -#endif diff --git a/src/Microsoft.AspNet.Routing/Owin/RouterMiddleware.cs b/src/Microsoft.AspNet.Routing/Owin/RouterMiddleware.cs index 21e4cf6904..d1298faf09 100644 --- a/src/Microsoft.AspNet.Routing/Owin/RouterMiddleware.cs +++ b/src/Microsoft.AspNet.Routing/Owin/RouterMiddleware.cs @@ -1,16 +1,13 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. -#if NET45 - -using System; -using System.Collections.Generic; using System.Threading.Tasks; +using Microsoft.AspNet.Abstractions; namespace Microsoft.AspNet.Routing.Owin { public class RouterMiddleware { - public RouterMiddleware(Func, Task> next, IRouteEngine engine) + public RouterMiddleware(RequestDelegate next, IRouteEngine engine) { Next = next; Engine = engine; @@ -22,13 +19,13 @@ namespace Microsoft.AspNet.Routing.Owin set; } - private Func, Task> Next + private RequestDelegate Next { get; set; } - public async Task Invoke(IDictionary context) + public async Task Invoke(HttpContext context) { if (!(await Engine.Invoke(context))) { @@ -37,5 +34,3 @@ namespace Microsoft.AspNet.Routing.Owin } } } - -#endif \ No newline at end of file diff --git a/src/Microsoft.AspNet.Routing/RouteContext.cs b/src/Microsoft.AspNet.Routing/RouteContext.cs index fb45e7dd6d..2b8df23106 100644 --- a/src/Microsoft.AspNet.Routing/RouteContext.cs +++ b/src/Microsoft.AspNet.Routing/RouteContext.cs @@ -1,19 +1,19 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. -using System.Collections.Generic; +using Microsoft.AspNet.Abstractions; namespace Microsoft.AspNet.Routing { public sealed class RouteContext { - public RouteContext(IDictionary context) + public RouteContext(HttpContext context) { Context = context; - RequestPath = (string)context["owin.RequestPath"]; + RequestPath = context.Request.Path.Value; } - public IDictionary Context + public HttpContext Context { get; private set; diff --git a/src/Microsoft.AspNet.Routing/RouteValues.cs b/src/Microsoft.AspNet.Routing/RouteValues.cs new file mode 100644 index 0000000000..da7dc4c85f --- /dev/null +++ b/src/Microsoft.AspNet.Routing/RouteValues.cs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. + +using System.Collections.Generic; + +namespace Microsoft.AspNet.Routing +{ + internal class RouteValues : IRouteValues + { + public RouteValues(IDictionary values) + { + Values = values; + } + + public IDictionary Values + { + get; + private set; + } + } +} diff --git a/src/Microsoft.AspNet.Routing/project.json b/src/Microsoft.AspNet.Routing/project.json index 9d789b9e65..ac508feb48 100644 --- a/src/Microsoft.AspNet.Routing/project.json +++ b/src/Microsoft.AspNet.Routing/project.json @@ -1,13 +1,10 @@ { "version": "0.1-alpha-*", "dependencies": { + "Microsoft.AspNet.Abstractions" : "0.1-alpha-*" }, "configurations": { - "net45": { - "dependencies": { - "Owin": "1.0" - } - }, + "net45": { }, "k10" : { } } } \ No newline at end of file