integrating routing with WebFX

This commit is contained in:
Ryan Nowak 2014-02-12 13:38:40 -08:00
parent 1f9d4969a7
commit 4a801b6c7a
13 changed files with 85 additions and 89 deletions

View File

@ -1,10 +1,14 @@
#if NET45

#if NET45
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Routing;
using Microsoft.AspNet.Routing.Owin;
using Microsoft.AspNet.Routing.Template;
using Microsoft.AspNet.Mvc.Startup;
using Owin;
@ -27,15 +31,13 @@ namespace MvcSample
var mvcServices = new MvcServices(appRoot);
var handler = ActivatorUtilities.CreateInstance<MvcHandler>(mvcServices.Services);
var router = builder.UseRouter();
builder.Run(async context =>
{
// Pretending to be routing
var routeData = new FakeRouteData(context);
await handler.ExecuteAsync(context, routeData);
});
var endpoint = ActivatorUtilities.CreateInstance<RouteEndpoint>(mvcServices.Services);
router.Add(new TemplateRoute(
endpoint,
"{controller}/{action}",
new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase){ { "controller", "Home"}, { "action", "Index" } }));
}
}
}

View File

@ -5,6 +5,7 @@
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
"Microsoft.AspNet.Razor" : "0.1-alpha-*",
"Microsoft.AspNet.Routing" : "0.1-alpha-*",
"Microsoft.AspNet.Mvc" : "",
"Microsoft.AspNet.Mvc.ModelBinding" : "",
"Microsoft.AspNet.Mvc.Rendering" : "",

View File

@ -1,40 +0,0 @@
using System;
using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.Mvc.Routing
{
// Move to routing middleware
public interface IRouteData
{
string GetRouteValue(string name);
}
public class FakeRouteData : IRouteData
{
private readonly string[] _parts;
public FakeRouteData(HttpContext context)
{
_parts = (context.Request.PathBase + context.Request.Path).Value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
}
public string GetRouteValue(string name)
{
if (name.Equals("controller", StringComparison.OrdinalIgnoreCase))
{
return GetPartOrDefault(0, "Home");
}
else if (name.Equals("action", StringComparison.OrdinalIgnoreCase))
{
return GetPartOrDefault(1, "Index");
}
return null;
}
private string GetPartOrDefault(int index, string defaultValue)
{
return index < _parts.Length ? _parts[index] : defaultValue;
}
}
}

View File

@ -1,28 +1,28 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Mvc.Routing;
namespace Microsoft.AspNet.Mvc
{
public class RequestContext
{
public RequestContext(HttpContext context, IRouteData routeData)
public RequestContext(HttpContext context, IDictionary<string, object> routeValues)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (routeData == null)
if (routeValues == null)
{
throw new ArgumentNullException("routeData");
throw new ArgumentNullException("routeValues");
}
HttpContext = context;
RouteData = routeData;
RouteValues = routeValues;
}
public virtual IRouteData RouteData { get; set; }
public virtual IDictionary<string, object> RouteValues { get; set; }
public virtual HttpContext HttpContext { get; set; }
}

View File

@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Mvc.Routing;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class ViewContext : RequestContext
{
public ViewContext(HttpContext context, IRouteData routeData, ViewData viewData) :
base(context, routeData)
public ViewContext(HttpContext context, IDictionary<string, object> routeValues, ViewData viewData) :
base(context, routeValues)
{
ViewData = viewData;
}

View File

@ -1,27 +0,0 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.Mvc.Routing;
namespace Microsoft.AspNet.Mvc
{
public class MvcHandler
{
private readonly IActionInvokerFactory _actionInvokerFactory;
public MvcHandler(IActionInvokerFactory actionInvokerFactory)
{
_actionInvokerFactory = actionInvokerFactory;
}
public Task ExecuteAsync(HttpContext context, IRouteData routeData)
{
var requestContext = new RequestContext(context, routeData);
var invoker = _actionInvokerFactory.CreateInvoker(requestContext);
return invoker.InvokeActionAsync();
}
}
}

View File

@ -3,6 +3,8 @@ using System.Collections.Generic;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.FileSystems;
using Microsoft.AspNet.Mvc.Razor;
using Microsoft.AspNet.Mvc.Routing;
using Microsoft.AspNet.Routing;
namespace Microsoft.AspNet.Mvc.Startup
{

View File

@ -5,6 +5,7 @@
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
"Microsoft.AspNet.FileSystems" : "0.1-alpha-*",
"Microsoft.AspNet.Razor" : "0.1-alpha-*",
"Microsoft.AspNet.Routing" : "0.1-alpha-*",
"Microsoft.AspNet.Mvc": "",
"Microsoft.AspNet.Mvc.Razor": "",
"Microsoft.AspNet.Mvc.ModelBinding" : "",

View File

@ -4,8 +4,8 @@
{
public RouteContext CreateDescriptor(RequestContext requestContext)
{
string controllerName = requestContext.RouteData.GetRouteValue("controller");
string actionName = requestContext.RouteData.GetRouteValue("action");
var controllerName = (string)requestContext.RouteValues["controller"];
var actionName = (string)requestContext.RouteValues["action"];
return new ControllerActionRouteContext
{

View File

@ -21,7 +21,11 @@ namespace Microsoft.AspNet.Mvc
public IActionInvoker CreateInvoker(RequestContext requestContext)
{
RouteContext routeContext = _routeContextProvider.CreateDescriptor(requestContext);
if (routeContext == null)
{
return null;
}
return _actionInvokerProvider.GetInvoker(requestContext, routeContext);
}
}

View File

@ -35,7 +35,7 @@ namespace Microsoft.AspNet.Mvc
context.HttpContext.Response.ContentType = "text/html";
using (var writer = new StreamWriter(context.HttpContext.Response.Body, Encoding.UTF8, 1024, leaveOpen: true))
{
var viewContext = new ViewContext(context.HttpContext, context.RouteData, ViewData)
var viewContext = new ViewContext(context.HttpContext, context.RouteValues, ViewData)
{
ServiceProvider = _serviceProvider
};

View File

@ -0,0 +1,52 @@

using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.Routing;
namespace Microsoft.AspNet.Mvc.Routing
{
public class RouteEndpoint : IRouteEndpoint
{
private readonly IServiceProvider _services;
private IActionInvokerFactory _actionInvokerFactory;
// Using service provider here to prevent ordering issues with configuration...
// IE: creating routes before configuring services, vice-versa.
public RouteEndpoint(IServiceProvider services)
{
_services = services;
}
private IActionInvokerFactory ActionInvokerFactory
{
get
{
if (_actionInvokerFactory == null)
{
_actionInvokerFactory = _services.GetService<IActionInvokerFactory>();
}
return _actionInvokerFactory;
}
}
public async Task<bool> Send(HttpContext context)
{
var routeValues = context.GetFeature<IRouteValues>();
var requestContext = new RequestContext(context, routeValues.Values);
var invoker = ActionInvokerFactory.CreateInvoker(requestContext);
if (invoker == null)
{
return false;
}
else
{
await invoker.InvokeActionAsync();
return true;
}
}
}
}

View File

@ -4,6 +4,7 @@
"Newtonsoft.Json": "5.0.8",
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
"Microsoft.AspNet.Routing": "0.1-alpha-*",
"Microsoft.AspNet.Mvc.ModelBinding": "",
"Microsoft.AspNet.Mvc.Rendering": ""
},