From 905e84ab0ab07e7ad16a93b7e84fc54c31220242 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Wed, 5 Mar 2014 17:17:12 -0800 Subject: [PATCH] Changes in WebFx for compatibility with routing --- samples/MvcSample.Web/Startup.cs | 26 ++++++++++------- samples/MvcSample/Startup.cs | 29 +++++++++---------- .../RouteEndpoint.cs => MvcApplication.cs} | 27 +++++++++-------- 3 files changed, 44 insertions(+), 38 deletions(-) rename src/Microsoft.AspNet.Mvc.Core/{Routing/RouteEndpoint.cs => MvcApplication.cs} (71%) diff --git a/samples/MvcSample.Web/Startup.cs b/samples/MvcSample.Web/Startup.cs index 0a3bfc1af9..bb2aab1a04 100644 --- a/samples/MvcSample.Web/Startup.cs +++ b/samples/MvcSample.Web/Startup.cs @@ -1,11 +1,9 @@ -using System; -using System.Collections.Generic; + using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.ConfigurationModel; using Microsoft.AspNet.DependencyInjection; using Microsoft.AspNet.Mvc; -using Microsoft.AspNet.Mvc.Routing; -using Microsoft.AspNet.Routing.Owin; +using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing.Template; using Microsoft.Net.Runtime; @@ -25,14 +23,20 @@ namespace MvcSample.Web var configuration = new Configuration(); var serviceProvider = new ServiceProvider().Add(MvcServices.GetDefaultServices(configuration, _env)); - var router = app.UseRouter(); + var routes = new RouteCollection() + { + DefaultHandler = new MvcApplication(serviceProvider), + }; - var endpoint = ActivatorUtilities.CreateInstance(serviceProvider); - - router.Routes.Add(new TemplateRoute( - endpoint, + routes.MapRoute( "{controller}/{action}", - new Dictionary(StringComparer.OrdinalIgnoreCase) { { "controller", "Home" }, { "action", "Index" } })); + new { controller = "Home", action = "Index" }); + + routes.MapRoute( + "{controller}", + new { controller = "Home" }); + + app.UseRouter(routes); } } -} \ No newline at end of file +} diff --git a/samples/MvcSample/Startup.cs b/samples/MvcSample/Startup.cs index d9fdff2743..4f9dc21257 100644 --- a/samples/MvcSample/Startup.cs +++ b/samples/MvcSample/Startup.cs @@ -1,12 +1,10 @@ #if NET45 using System; -using System.Collections.Generic; using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.ConfigurationModel; using Microsoft.AspNet.DependencyInjection; using Microsoft.AspNet.Mvc; -using Microsoft.AspNet.Mvc.Routing; -using Microsoft.AspNet.Routing.Owin; +using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing.Template; using Microsoft.Net.Runtime; using Owin; @@ -39,20 +37,21 @@ namespace MvcSample var services = MvcServices.GetDefaultServices(configuration, _env); var serviceProvider = new ServiceProvider().Add(services); - var router = builder.UseRouter(); + var routes = new RouteCollection() + { + DefaultHandler = new MvcApplication(serviceProvider), + }; - var endpoint = ActivatorUtilities.CreateInstance(serviceProvider); - - router.Routes.Add(new TemplateRoute( - endpoint, - "{controller}", - new Dictionary(StringComparer.OrdinalIgnoreCase) { { "controller", "Home" } })); - - router.Routes.Add(new TemplateRoute( - endpoint, + routes.MapRoute( "{controller}/{action}", - new Dictionary(StringComparer.OrdinalIgnoreCase) { { "controller", "Home" }, { "action", "Index" } })); + new { controller = "Home", action = "Index" }); + + routes.MapRoute( + "{controller}", + new { controller = "Home" }); + + builder.UseRouter(routes); } } } -#endif \ No newline at end of file +#endif diff --git a/src/Microsoft.AspNet.Mvc.Core/Routing/RouteEndpoint.cs b/src/Microsoft.AspNet.Mvc.Core/MvcApplication.cs similarity index 71% rename from src/Microsoft.AspNet.Mvc.Core/Routing/RouteEndpoint.cs rename to src/Microsoft.AspNet.Mvc.Core/MvcApplication.cs index 731e88ce0d..9284a8ae00 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Routing/RouteEndpoint.cs +++ b/src/Microsoft.AspNet.Mvc.Core/MvcApplication.cs @@ -1,13 +1,12 @@  using System; using System.Threading.Tasks; -using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.DependencyInjection; using Microsoft.AspNet.Routing; -namespace Microsoft.AspNet.Mvc.Routing +namespace Microsoft.AspNet.Mvc { - public class RouteEndpoint : IRouteEndpoint + public class MvcApplication : IRouter { private readonly IServiceProvider _services; private IActionInvokerFactory _actionInvokerFactory; @@ -15,7 +14,7 @@ namespace Microsoft.AspNet.Mvc.Routing // Using service provider here to prevent ordering issues with configuration... // IE: creating routes before configuring services, vice-versa. - public RouteEndpoint(IServiceProvider services) + public MvcApplication(IServiceProvider services) { _services = services; } @@ -46,20 +45,24 @@ namespace Microsoft.AspNet.Mvc.Routing } } - public async Task Send(HttpContext context) + public void BindPath(BindPathContext context) { - var routeValues = context.GetFeature(); - var requestContext = new RequestContext(context, routeValues.Values); + // For now just allow any values to target this application. + context.IsBound = true; + } + + public async Task RouteAsync(RouteContext context) + { + var requestContext = new RequestContext(context.HttpContext, context.Values); var actionDescriptor = await ActionSelector.SelectAsync(requestContext); - if (actionDescriptor == null) { - return false; + return; } - var invoker = ActionInvokerFactory.CreateInvoker(new ActionContext(context, routeValues.Values, actionDescriptor)); - + var actionContext = new ActionContext(context.HttpContext, context.Values, actionDescriptor); + var invoker = ActionInvokerFactory.CreateInvoker(actionContext); if (invoker == null) { var ex = new InvalidOperationException("Could not instantiate invoker for the actionDescriptor"); @@ -72,7 +75,7 @@ namespace Microsoft.AspNet.Mvc.Routing await invoker.InvokeActionAsync(); - return true; + context.IsHandled = true; } } }