integrating abstractions with routing

This commit is contained in:
Ryan Nowak 2014-02-03 15:56:44 -08:00
parent 5676745562
commit c796188231
13 changed files with 96 additions and 41 deletions

View File

@ -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;
}
}
}

View File

@ -3,6 +3,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.PipelineCore.Owin;
using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing;
namespace RoutingSample namespace RoutingSample
@ -16,9 +18,10 @@ namespace RoutingSample
_appFunc = appFunc; _appFunc = appFunc;
} }
public Task Invoke(IDictionary<string, object> context) public Task Invoke(HttpContext context)
{ {
return _appFunc(context); var owinContext = context.GetFeature<ICanHasOwinEnvironment>().Environment;
return _appFunc(owinContext);
} }
} }
} }

View File

@ -6,6 +6,10 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Routing.Owin; using Microsoft.AspNet.Routing.Owin;
using Owin; using Owin;
using Microsoft.AspNet.PipelineCore.Owin;
using Microsoft.AspNet.Routing;
using System;
using Microsoft.AspNet.Abstractions;
namespace RoutingSample namespace RoutingSample
{ {
@ -15,6 +19,7 @@ namespace RoutingSample
{ {
var routes = appBuilder.UseRouter(); var routes = appBuilder.UseRouter();
OwinRouteEndpoint endpoint1 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "match1")); OwinRouteEndpoint endpoint1 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "match1"));
OwinRouteEndpoint endpoint2 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "Hello, World!")); OwinRouteEndpoint endpoint2 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "Hello, World!"));

View File

@ -1,16 +1,21 @@
{ {
"version": "0.1-alpha-*", "version": "0.1-alpha-*",
"dependencies": { "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" : "" "Microsoft.AspNet.Routing" : ""
}, },
"configurations": { "configurations": {
"net45": { "net45": {
"dependencies": { "dependencies": {
"Owin": "1.0", "Owin": "1.0",
"Microsoft.Owin" : "2.1.0", "Microsoft.Owin" : "2.1.0",
"Microsoft.Owin.Host.HttpListener" : "2.1.0", "Microsoft.Owin.Host.HttpListener" : "2.1.0",
"Microsoft.Owin.Hosting" : "2.1.0" "Microsoft.Owin.Hosting" : "2.1.0"
} }
} }
} }
} }

View File

@ -1,12 +1,11 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. // 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 System.Threading.Tasks;
using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.Routing namespace Microsoft.AspNet.Routing
{ {
internal class DefaultRouteEngine : IRouteEngine public class DefaultRouteEngine : IRouteEngine
{ {
public DefaultRouteEngine(IRouteCollection routes) public DefaultRouteEngine(IRouteCollection routes)
{ {
@ -19,7 +18,7 @@ namespace Microsoft.AspNet.Routing
private set; private set;
} }
public async Task<bool> Invoke(IDictionary<string, object> context) public async Task<bool> Invoke(HttpContext context)
{ {
RouteContext routeContext = new RouteContext(context); RouteContext routeContext = new RouteContext(context);
@ -30,6 +29,8 @@ namespace Microsoft.AspNet.Routing
RouteMatch match = route.Match(routeContext); RouteMatch match = route.Match(routeContext);
if (match != null) if (match != null)
{ {
context.SetFeature<IRouteValues>(new RouteValues(match.Values));
await match.Endpoint.Invoke(context); await match.Endpoint.Invoke(context);
return true; return true;
} }

View File

@ -1,12 +1,12 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. // 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 System.Threading.Tasks;
using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.Routing namespace Microsoft.AspNet.Routing
{ {
public interface IRouteEndpoint public interface IRouteEndpoint
{ {
Task Invoke(IDictionary<string, object> context); Task Invoke(HttpContext context);
} }
} }

View File

@ -1,12 +1,12 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. // 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 System.Threading.Tasks;
using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.Routing namespace Microsoft.AspNet.Routing
{ {
public interface IRouteEngine public interface IRouteEngine
{ {
Task<bool> Invoke(IDictionary<string, object> context); Task<bool> Invoke(HttpContext context);
} }
} }

View File

@ -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<string, object> Values
{
get;
}
}
}

View File

@ -1,23 +1,20 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
#if NET45 using Microsoft.AspNet.Abstractions;
using Owin;
namespace Microsoft.AspNet.Routing.Owin 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 routes = new DefaultRouteCollection();
var engine = new DefaultRouteEngine(routes); var engine = new DefaultRouteEngine(routes);
builder.Use(typeof(RouterMiddleware), engine); builder.Use((next) => new RouterMiddleware(next, engine).Invoke);
return routes; return routes;
} }
} }
} }
#endif

View File

@ -1,16 +1,13 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. // 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 System.Threading.Tasks;
using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.Routing.Owin namespace Microsoft.AspNet.Routing.Owin
{ {
public class RouterMiddleware public class RouterMiddleware
{ {
public RouterMiddleware(Func<IDictionary<string, object>, Task> next, IRouteEngine engine) public RouterMiddleware(RequestDelegate next, IRouteEngine engine)
{ {
Next = next; Next = next;
Engine = engine; Engine = engine;
@ -22,13 +19,13 @@ namespace Microsoft.AspNet.Routing.Owin
set; set;
} }
private Func<IDictionary<string, object>, Task> Next private RequestDelegate Next
{ {
get; get;
set; set;
} }
public async Task Invoke(IDictionary<string, object> context) public async Task Invoke(HttpContext context)
{ {
if (!(await Engine.Invoke(context))) if (!(await Engine.Invoke(context)))
{ {
@ -37,5 +34,3 @@ namespace Microsoft.AspNet.Routing.Owin
} }
} }
} }
#endif

View File

@ -1,19 +1,19 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. // 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 namespace Microsoft.AspNet.Routing
{ {
public sealed class RouteContext public sealed class RouteContext
{ {
public RouteContext(IDictionary<string, object> context) public RouteContext(HttpContext context)
{ {
Context = context; Context = context;
RequestPath = (string)context["owin.RequestPath"]; RequestPath = context.Request.Path.Value;
} }
public IDictionary<string, object> Context public HttpContext Context
{ {
get; get;
private set; private set;

View File

@ -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<string, object> values)
{
Values = values;
}
public IDictionary<string, object> Values
{
get;
private set;
}
}
}

View File

@ -1,13 +1,10 @@
{ {
"version": "0.1-alpha-*", "version": "0.1-alpha-*",
"dependencies": { "dependencies": {
"Microsoft.AspNet.Abstractions" : "0.1-alpha-*"
}, },
"configurations": { "configurations": {
"net45": { "net45": { },
"dependencies": {
"Owin": "1.0"
}
},
"k10" : { } "k10" : { }
} }
} }