From 0eb5ff0b87d15acc5ac9bcd6679a9367bc8dca3e 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 --- Routing.sln | 7 ++++++ .../RoutingSample/HttpContextRouteEndpoint.cs | 25 +++++++++++++++++++ samples/RoutingSample/OwinRouteEndpoint.cs | 8 +++++- samples/RoutingSample/Program.cs | 2 ++ samples/RoutingSample/Startup.cs | 21 ++++++++++++---- samples/RoutingSample/project.json | 9 +++---- .../DefaultRouteEngine.cs | 7 ++++-- .../IRouteEndpoint.cs | 2 +- src/Microsoft.AspNet.Routing/RouteContext.cs | 2 +- src/Microsoft.AspNet.Routing/RouteMatch.cs | 2 +- src/Microsoft.AspNet.Routing/RouteValues.cs | 2 +- 11 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 samples/RoutingSample/HttpContextRouteEndpoint.cs diff --git a/Routing.sln b/Routing.sln index 9f44962866..3009f0f199 100644 --- a/Routing.sln +++ b/Routing.sln @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{95359B4B-4 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.Routing.Tests.net45", "test\Microsoft.AspNet.Routing.Tests\Microsoft.AspNet.Routing.Tests.net45.csproj", "{3775A966-0876-45C3-A67F-0E544BC48D55}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoutingSample.k10", "samples\RoutingSample\RoutingSample.k10.csproj", "{1BB31C1A-C6F8-4C00-BD30-92EF775276BE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {3775A966-0876-45C3-A67F-0E544BC48D55}.Debug|Any CPU.Build.0 = Debug|Any CPU {3775A966-0876-45C3-A67F-0E544BC48D55}.Release|Any CPU.ActiveCfg = Release|Any CPU {3775A966-0876-45C3-A67F-0E544BC48D55}.Release|Any CPU.Build.0 = Release|Any CPU + {1BB31C1A-C6F8-4C00-BD30-92EF775276BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1BB31C1A-C6F8-4C00-BD30-92EF775276BE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1BB31C1A-C6F8-4C00-BD30-92EF775276BE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1BB31C1A-C6F8-4C00-BD30-92EF775276BE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -47,6 +53,7 @@ Global {16537084-C16D-4885-B90F-18BAC218F85B} = {0E966C37-7334-4D96-AAF6-9F49FBD166E3} {1366B89C-F603-4088-B313-FA053C51BAC6} = {0E966C37-7334-4D96-AAF6-9F49FBD166E3} {121DC7B4-E29B-45E1-BF7E-314842F99A0D} = {C3ADD55B-B9C7-4061-8AD4-6A70D1AE3B2E} + {1BB31C1A-C6F8-4C00-BD30-92EF775276BE} = {C3ADD55B-B9C7-4061-8AD4-6A70D1AE3B2E} {3775A966-0876-45C3-A67F-0E544BC48D55} = {95359B4B-4C85-4B44-A75B-0621905C4CF6} EndGlobalSection EndGlobal diff --git a/samples/RoutingSample/HttpContextRouteEndpoint.cs b/samples/RoutingSample/HttpContextRouteEndpoint.cs new file mode 100644 index 0000000000..8e38c61cb2 --- /dev/null +++ b/samples/RoutingSample/HttpContextRouteEndpoint.cs @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. + +using System; +using System.Threading.Tasks; +using Microsoft.AspNet.Abstractions; +using Microsoft.AspNet.Routing; + +namespace RoutingSample +{ + public class HttpContextRouteEndpoint : IRouteEndpoint + { + private readonly Func _appFunc; + + public HttpContextRouteEndpoint(Func appFunc) + { + _appFunc = appFunc; + } + + public async Task Send(HttpContext context) + { + await _appFunc(context); + return true; + } + } +} diff --git a/samples/RoutingSample/OwinRouteEndpoint.cs b/samples/RoutingSample/OwinRouteEndpoint.cs index 734a81b9da..fab5337248 100644 --- a/samples/RoutingSample/OwinRouteEndpoint.cs +++ b/samples/RoutingSample/OwinRouteEndpoint.cs @@ -1,5 +1,7 @@ // 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; @@ -18,10 +20,14 @@ namespace RoutingSample _appFunc = appFunc; } - public Task Invoke(HttpContext context) + public async Task Send(HttpContext context) { var owinContext = context.GetFeature().Environment; return _appFunc(owinContext); + await _appFunc(owinContext); + return true; } } } + +#endif diff --git a/samples/RoutingSample/Program.cs b/samples/RoutingSample/Program.cs index 9ae2ef4a73..b39d0aefbe 100644 --- a/samples/RoutingSample/Program.cs +++ b/samples/RoutingSample/Program.cs @@ -1,5 +1,6 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +#if NET45 using System; using Microsoft.Owin.Hosting; @@ -20,3 +21,4 @@ namespace RoutingSample } } } +#endif \ No newline at end of file diff --git a/samples/RoutingSample/Startup.cs b/samples/RoutingSample/Startup.cs index 610d56f619..8a8eea7dc3 100644 --- a/samples/RoutingSample/Startup.cs +++ b/samples/RoutingSample/Startup.cs @@ -1,9 +1,12 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. +#if NET45 + using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; +using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.Routing.Owin; using Owin; using Microsoft.AspNet.PipelineCore.Owin; @@ -15,13 +18,19 @@ namespace RoutingSample { internal class Startup { - public void Configuration(IAppBuilder appBuilder) + public void Configuration(IAppBuilder builder) { - var routes = appBuilder.UseRouter(); - + builder.UseErrorPage(); - OwinRouteEndpoint endpoint1 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "match1")); - OwinRouteEndpoint endpoint2 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "Hello, World!")); + builder.UseBuilder(ConfigureRoutes); + } + + private void ConfigureRoutes(IBuilder builder) + { + var routes = builder.UseRouter(); + + var endpoint1 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "match1")); + var endpoint2 = new HttpContextRouteEndpoint(async (context) => await context.Response.WriteAsync("Hello, World!")); routes.Add(new PrefixRoute(endpoint1, "api/store")); routes.Add(new PrefixRoute(endpoint1, "api/checkout")); @@ -38,3 +47,5 @@ namespace RoutingSample } } } + +#endif diff --git a/samples/RoutingSample/project.json b/samples/RoutingSample/project.json index 35f33aab61..eff9d5df3e 100644 --- a/samples/RoutingSample/project.json +++ b/samples/RoutingSample/project.json @@ -3,9 +3,6 @@ "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": { @@ -13,9 +10,11 @@ "dependencies": { "Owin": "1.0", "Microsoft.Owin" : "2.1.0", + "Microsoft.Owin.Diagnostics" : "2.1.0", "Microsoft.Owin.Host.HttpListener" : "2.1.0", "Microsoft.Owin.Hosting" : "2.1.0" } - } + }, + "k10": {} } -} \ No newline at end of file +} diff --git a/src/Microsoft.AspNet.Routing/DefaultRouteEngine.cs b/src/Microsoft.AspNet.Routing/DefaultRouteEngine.cs index 07c4a3f517..2f76d5822c 100644 --- a/src/Microsoft.AspNet.Routing/DefaultRouteEngine.cs +++ b/src/Microsoft.AspNet.Routing/DefaultRouteEngine.cs @@ -31,8 +31,11 @@ namespace Microsoft.AspNet.Routing { context.SetFeature(new RouteValues(match.Values)); - await match.Endpoint.Invoke(context); - return true; + var accepted = await match.Endpoint.Send(context); + if (accepted) + { + return true; + } } } diff --git a/src/Microsoft.AspNet.Routing/IRouteEndpoint.cs b/src/Microsoft.AspNet.Routing/IRouteEndpoint.cs index b5ebcc284d..df74ee0259 100644 --- a/src/Microsoft.AspNet.Routing/IRouteEndpoint.cs +++ b/src/Microsoft.AspNet.Routing/IRouteEndpoint.cs @@ -7,6 +7,6 @@ namespace Microsoft.AspNet.Routing { public interface IRouteEndpoint { - Task Invoke(HttpContext context); + Task Send(HttpContext context); } } diff --git a/src/Microsoft.AspNet.Routing/RouteContext.cs b/src/Microsoft.AspNet.Routing/RouteContext.cs index 2b8df23106..48eb5157aa 100644 --- a/src/Microsoft.AspNet.Routing/RouteContext.cs +++ b/src/Microsoft.AspNet.Routing/RouteContext.cs @@ -4,7 +4,7 @@ using Microsoft.AspNet.Abstractions; namespace Microsoft.AspNet.Routing { - public sealed class RouteContext + public class RouteContext { public RouteContext(HttpContext context) { diff --git a/src/Microsoft.AspNet.Routing/RouteMatch.cs b/src/Microsoft.AspNet.Routing/RouteMatch.cs index 3bea932dd2..92d5ce20c0 100644 --- a/src/Microsoft.AspNet.Routing/RouteMatch.cs +++ b/src/Microsoft.AspNet.Routing/RouteMatch.cs @@ -8,7 +8,7 @@ namespace Microsoft.AspNet.Routing /// The result of matching a route. Includes an to invoke and an optional collection of /// captured values. /// - public sealed class RouteMatch + public class RouteMatch { public RouteMatch(IRouteEndpoint endpoint) : this(endpoint, null) diff --git a/src/Microsoft.AspNet.Routing/RouteValues.cs b/src/Microsoft.AspNet.Routing/RouteValues.cs index da7dc4c85f..01acc8e991 100644 --- a/src/Microsoft.AspNet.Routing/RouteValues.cs +++ b/src/Microsoft.AspNet.Routing/RouteValues.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; namespace Microsoft.AspNet.Routing { - internal class RouteValues : IRouteValues + public class RouteValues : IRouteValues { public RouteValues(IDictionary values) {