integrating routing with WebFX
This commit is contained in:
parent
1f9d4969a7
commit
4a801b6c7a
|
|
@ -1,10 +1,14 @@
|
||||||
#if NET45
|
|
||||||
|
#if NET45
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.AspNet.Abstractions;
|
using Microsoft.AspNet.Abstractions;
|
||||||
using Microsoft.AspNet.DependencyInjection;
|
using Microsoft.AspNet.DependencyInjection;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
using Microsoft.AspNet.Mvc.Routing;
|
using Microsoft.AspNet.Mvc.Routing;
|
||||||
|
using Microsoft.AspNet.Routing.Owin;
|
||||||
|
using Microsoft.AspNet.Routing.Template;
|
||||||
using Microsoft.AspNet.Mvc.Startup;
|
using Microsoft.AspNet.Mvc.Startup;
|
||||||
using Owin;
|
using Owin;
|
||||||
|
|
||||||
|
|
@ -27,15 +31,13 @@ namespace MvcSample
|
||||||
|
|
||||||
var mvcServices = new MvcServices(appRoot);
|
var mvcServices = new MvcServices(appRoot);
|
||||||
|
|
||||||
var handler = ActivatorUtilities.CreateInstance<MvcHandler>(mvcServices.Services);
|
var router = builder.UseRouter();
|
||||||
|
|
||||||
builder.Run(async context =>
|
var endpoint = ActivatorUtilities.CreateInstance<RouteEndpoint>(mvcServices.Services);
|
||||||
{
|
router.Add(new TemplateRoute(
|
||||||
// Pretending to be routing
|
endpoint,
|
||||||
var routeData = new FakeRouteData(context);
|
"{controller}/{action}",
|
||||||
|
new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase){ { "controller", "Home"}, { "action", "Index" } }));
|
||||||
await handler.ExecuteAsync(context, routeData);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
|
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
|
||||||
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
|
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
|
||||||
"Microsoft.AspNet.Razor" : "0.1-alpha-*",
|
"Microsoft.AspNet.Razor" : "0.1-alpha-*",
|
||||||
|
"Microsoft.AspNet.Routing" : "0.1-alpha-*",
|
||||||
"Microsoft.AspNet.Mvc" : "",
|
"Microsoft.AspNet.Mvc" : "",
|
||||||
"Microsoft.AspNet.Mvc.ModelBinding" : "",
|
"Microsoft.AspNet.Mvc.ModelBinding" : "",
|
||||||
"Microsoft.AspNet.Mvc.Rendering" : "",
|
"Microsoft.AspNet.Mvc.Rendering" : "",
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,28 +1,28 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNet.Abstractions;
|
using Microsoft.AspNet.Abstractions;
|
||||||
using Microsoft.AspNet.Mvc.Routing;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc
|
namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
public class RequestContext
|
public class RequestContext
|
||||||
{
|
{
|
||||||
public RequestContext(HttpContext context, IRouteData routeData)
|
public RequestContext(HttpContext context, IDictionary<string, object> routeValues)
|
||||||
{
|
{
|
||||||
if (context == null)
|
if (context == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException("context");
|
throw new ArgumentNullException("context");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (routeData == null)
|
if (routeValues == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException("routeData");
|
throw new ArgumentNullException("routeValues");
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpContext = context;
|
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; }
|
public virtual HttpContext HttpContext { get; set; }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNet.Abstractions;
|
using Microsoft.AspNet.Abstractions;
|
||||||
using Microsoft.AspNet.Mvc.Routing;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.ModelBinding
|
namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
{
|
{
|
||||||
public class ViewContext : RequestContext
|
public class ViewContext : RequestContext
|
||||||
{
|
{
|
||||||
public ViewContext(HttpContext context, IRouteData routeData, ViewData viewData) :
|
public ViewContext(HttpContext context, IDictionary<string, object> routeValues, ViewData viewData) :
|
||||||
base(context, routeData)
|
base(context, routeValues)
|
||||||
{
|
{
|
||||||
ViewData = viewData;
|
ViewData = viewData;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -3,6 +3,8 @@ using System.Collections.Generic;
|
||||||
using Microsoft.AspNet.DependencyInjection;
|
using Microsoft.AspNet.DependencyInjection;
|
||||||
using Microsoft.AspNet.FileSystems;
|
using Microsoft.AspNet.FileSystems;
|
||||||
using Microsoft.AspNet.Mvc.Razor;
|
using Microsoft.AspNet.Mvc.Razor;
|
||||||
|
using Microsoft.AspNet.Mvc.Routing;
|
||||||
|
using Microsoft.AspNet.Routing;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.Startup
|
namespace Microsoft.AspNet.Mvc.Startup
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
|
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
|
||||||
"Microsoft.AspNet.FileSystems" : "0.1-alpha-*",
|
"Microsoft.AspNet.FileSystems" : "0.1-alpha-*",
|
||||||
"Microsoft.AspNet.Razor" : "0.1-alpha-*",
|
"Microsoft.AspNet.Razor" : "0.1-alpha-*",
|
||||||
|
"Microsoft.AspNet.Routing" : "0.1-alpha-*",
|
||||||
"Microsoft.AspNet.Mvc": "",
|
"Microsoft.AspNet.Mvc": "",
|
||||||
"Microsoft.AspNet.Mvc.Razor": "",
|
"Microsoft.AspNet.Mvc.Razor": "",
|
||||||
"Microsoft.AspNet.Mvc.ModelBinding" : "",
|
"Microsoft.AspNet.Mvc.ModelBinding" : "",
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@
|
||||||
{
|
{
|
||||||
public RouteContext CreateDescriptor(RequestContext requestContext)
|
public RouteContext CreateDescriptor(RequestContext requestContext)
|
||||||
{
|
{
|
||||||
string controllerName = requestContext.RouteData.GetRouteValue("controller");
|
var controllerName = (string)requestContext.RouteValues["controller"];
|
||||||
string actionName = requestContext.RouteData.GetRouteValue("action");
|
var actionName = (string)requestContext.RouteValues["action"];
|
||||||
|
|
||||||
return new ControllerActionRouteContext
|
return new ControllerActionRouteContext
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,11 @@ namespace Microsoft.AspNet.Mvc
|
||||||
public IActionInvoker CreateInvoker(RequestContext requestContext)
|
public IActionInvoker CreateInvoker(RequestContext requestContext)
|
||||||
{
|
{
|
||||||
RouteContext routeContext = _routeContextProvider.CreateDescriptor(requestContext);
|
RouteContext routeContext = _routeContextProvider.CreateDescriptor(requestContext);
|
||||||
|
if (routeContext == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return _actionInvokerProvider.GetInvoker(requestContext, routeContext);
|
return _actionInvokerProvider.GetInvoker(requestContext, routeContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
context.HttpContext.Response.ContentType = "text/html";
|
context.HttpContext.Response.ContentType = "text/html";
|
||||||
using (var writer = new StreamWriter(context.HttpContext.Response.Body, Encoding.UTF8, 1024, leaveOpen: true))
|
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
|
ServiceProvider = _serviceProvider
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
"Newtonsoft.Json": "5.0.8",
|
"Newtonsoft.Json": "5.0.8",
|
||||||
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
|
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
|
||||||
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
|
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
|
||||||
|
"Microsoft.AspNet.Routing": "0.1-alpha-*",
|
||||||
"Microsoft.AspNet.Mvc.ModelBinding": "",
|
"Microsoft.AspNet.Mvc.ModelBinding": "",
|
||||||
"Microsoft.AspNet.Mvc.Rendering": ""
|
"Microsoft.AspNet.Mvc.Rendering": ""
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue