integrating abstractions with routing

This commit is contained in:
Ryan Nowak 2014-02-03 15:56:44 -08:00
parent c796188231
commit 0eb5ff0b87
11 changed files with 70 additions and 17 deletions

View File

@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{95359B4B-4
EndProject 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}" 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 EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoutingSample.k10", "samples\RoutingSample\RoutingSample.k10.csproj", "{1BB31C1A-C6F8-4C00-BD30-92EF775276BE}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{3775A966-0876-45C3-A67F-0E544BC48D55}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -47,6 +53,7 @@ Global
{16537084-C16D-4885-B90F-18BAC218F85B} = {0E966C37-7334-4D96-AAF6-9F49FBD166E3} {16537084-C16D-4885-B90F-18BAC218F85B} = {0E966C37-7334-4D96-AAF6-9F49FBD166E3}
{1366B89C-F603-4088-B313-FA053C51BAC6} = {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} {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} {3775A966-0876-45C3-A67F-0E544BC48D55} = {95359B4B-4C85-4B44-A75B-0621905C4CF6}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@ -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<HttpContext, Task> _appFunc;
public HttpContextRouteEndpoint(Func<HttpContext, Task> appFunc)
{
_appFunc = appFunc;
}
public async Task<bool> Send(HttpContext context)
{
await _appFunc(context);
return true;
}
}
}

View File

@ -1,5 +1,7 @@
// 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -18,10 +20,14 @@ namespace RoutingSample
_appFunc = appFunc; _appFunc = appFunc;
} }
public Task Invoke(HttpContext context) public async Task<bool> Send(HttpContext context)
{ {
var owinContext = context.GetFeature<ICanHasOwinEnvironment>().Environment; var owinContext = context.GetFeature<ICanHasOwinEnvironment>().Environment;
return _appFunc(owinContext); return _appFunc(owinContext);
await _appFunc(owinContext);
return true;
} }
} }
} }
#endif

View File

@ -1,5 +1,6 @@
// 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;
using Microsoft.Owin.Hosting; using Microsoft.Owin.Hosting;
@ -20,3 +21,4 @@ namespace RoutingSample
} }
} }
} }
#endif

View File

@ -1,9 +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.
#if NET45
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Routing.Owin; using Microsoft.AspNet.Routing.Owin;
using Owin; using Owin;
using Microsoft.AspNet.PipelineCore.Owin; using Microsoft.AspNet.PipelineCore.Owin;
@ -15,13 +18,19 @@ namespace RoutingSample
{ {
internal class Startup internal class Startup
{ {
public void Configuration(IAppBuilder appBuilder) public void Configuration(IAppBuilder builder)
{ {
var routes = appBuilder.UseRouter(); builder.UseErrorPage();
builder.UseBuilder(ConfigureRoutes);
}
OwinRouteEndpoint endpoint1 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "match1")); private void ConfigureRoutes(IBuilder builder)
OwinRouteEndpoint endpoint2 = new OwinRouteEndpoint(async (context) => await WriteToBodyAsync(context, "Hello, World!")); {
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/store"));
routes.Add(new PrefixRoute(endpoint1, "api/checkout")); routes.Add(new PrefixRoute(endpoint1, "api/checkout"));
@ -38,3 +47,5 @@ namespace RoutingSample
} }
} }
} }
#endif

View File

@ -3,9 +3,6 @@
"dependencies": { "dependencies": {
"Microsoft.AspNet.Abstractions" : "0.1-alpha-*", "Microsoft.AspNet.Abstractions" : "0.1-alpha-*",
"Microsoft.AspNet.AppBuilderSupport": "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": {
@ -13,9 +10,11 @@
"dependencies": { "dependencies": {
"Owin": "1.0", "Owin": "1.0",
"Microsoft.Owin" : "2.1.0", "Microsoft.Owin" : "2.1.0",
"Microsoft.Owin.Diagnostics" : "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"
} }
} },
"k10": {}
} }
} }

View File

@ -31,8 +31,11 @@ namespace Microsoft.AspNet.Routing
{ {
context.SetFeature<IRouteValues>(new RouteValues(match.Values)); context.SetFeature<IRouteValues>(new RouteValues(match.Values));
await match.Endpoint.Invoke(context); var accepted = await match.Endpoint.Send(context);
return true; if (accepted)
{
return true;
}
} }
} }

View File

@ -7,6 +7,6 @@ namespace Microsoft.AspNet.Routing
{ {
public interface IRouteEndpoint public interface IRouteEndpoint
{ {
Task Invoke(HttpContext context); Task<bool> Send(HttpContext context);
} }
} }

View File

@ -4,7 +4,7 @@ using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.Routing namespace Microsoft.AspNet.Routing
{ {
public sealed class RouteContext public class RouteContext
{ {
public RouteContext(HttpContext context) public RouteContext(HttpContext context)
{ {

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNet.Routing
/// The result of matching a route. Includes an <see cref="IRouteEndpoint"/> to invoke and an optional collection of /// The result of matching a route. Includes an <see cref="IRouteEndpoint"/> to invoke and an optional collection of
/// captured values. /// captured values.
/// </summary> /// </summary>
public sealed class RouteMatch public class RouteMatch
{ {
public RouteMatch(IRouteEndpoint endpoint) public RouteMatch(IRouteEndpoint endpoint)
: this(endpoint, null) : this(endpoint, null)

View File

@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace Microsoft.AspNet.Routing namespace Microsoft.AspNet.Routing
{ {
internal class RouteValues : IRouteValues public class RouteValues : IRouteValues
{ {
public RouteValues(IDictionary<string, object> values) public RouteValues(IDictionary<string, object> values)
{ {