在 ASP.NET 5 (vNext) MVC 6 中实现自定义路由器
Posted
技术标签:
【中文标题】在 ASP.NET 5 (vNext) MVC 6 中实现自定义路由器【英文标题】:Imlementing a Custom IRouter in ASP.NET 5 (vNext) MVC 6 【发布时间】:2015-09-15 09:16:56 【问题描述】:我正在尝试将this sample RouteBase implementation 转换为与MVC 6 一起使用。我已经按照the example in the Routing project 解决了大部分问题,但是我对如何从该方法返回异步Task
感到困惑。我真的不在乎它是否真的是异步的(为任何可以提供该答案的人干杯),现在我只想让它正常运行。
我的传出路由正常运行(意思是ActionLink
在我输入路由值时可以正常工作)。问题出在RouteAsync
方法上。
public Task RouteAsync(RouteContext context)
var requestPath = context.HttpContext.Request.Path.Value;
if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
// Trim the leading slash
requestPath = requestPath.Substring(1);
// Get the page that matches.
var page = GetPageList()
.Where(x => x.VirtualPath.Equals(requestPath))
.FirstOrDefault();
// If we got back a null value set, that means the URI did not match
if (page != null)
var routeData = new RouteData();
// This doesn't work
//var routeData = new RouteData(context.RouteData);
// This doesn't work
//routeData.Routers.Add(this);
// This doesn't work
//routeData.Routers.Add(new MvcRouteHandler());
// TODO: You might want to use the page object (from the database) to
// get both the controller and action, and possibly even an area.
// Alternatively, you could create a route for each table and hard-code
// this information.
routeData.Values["controller"] = "CustomPage";
routeData.Values["action"] = "Details";
// This will be the primary key of the database row.
// It might be an integer or a GUID.
routeData.Values["id"] = page.Id;
context.RouteData = routeData;
// When there is a match, the code executes to here
context.IsHandled = true;
// This test works
//await context.HttpContext.Response.WriteAsync("Hello there");
// This doesn't work
//return Task.FromResult(routeData);
// This doesn't work
//return Task.FromResult(context);
// This satisfies the return statement, but
// I'm not sure it is the right thing to return.
return Task.FromResult(0);
当有匹配时,整个方法一直运行到最后。但是当它完成执行时,它不会调用CustomPage
控制器的Details
方法,因为它应该。我只是在浏览器中得到一个空白页面。
我添加了 WriteAsync
行,就像在 this post 中所做的那样,它将 Hello there
写入空白页,但我不明白为什么 MVC 不调用我的控制器(在以前的版本中,这在没有拴住)。不幸的是,除了如何实现IRouter
或INamedRouter
之外,该帖子涵盖了路由的所有部分。
如何使RouteAsync
方法起作用?
整个 CustomRoute 实现
using Microsoft.AspNet.Routing;
using Microsoft.Framework.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
public class PageInfo
// VirtualPath should not have a leading slash
// example: events/conventions/mycon
public string VirtualPath get; set;
public int Id get; set;
public interface ICustomRoute : IRouter
public class CustomRoute : ICustomRoute
private readonly IMemoryCache cache;
private object synclock = new object();
public CustomRoute(IMemoryCache cache)
this.cache = cache;
public Task RouteAsync(RouteContext context)
var requestPath = context.HttpContext.Request.Path.Value;
if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
// Trim the leading slash
requestPath = requestPath.Substring(1);
// Get the page that matches.
var page = GetPageList()
.Where(x => x.VirtualPath.Equals(requestPath))
.FirstOrDefault();
// If we got back a null value set, that means the URI did not match
if (page != null)
var routeData = new RouteData();
// TODO: You might want to use the page object (from the database) to
// get both the controller and action, and possibly even an area.
// Alternatively, you could create a route for each table and hard-code
// this information.
routeData.Values["controller"] = "CustomPage";
routeData.Values["action"] = "Details";
// This will be the primary key of the database row.
// It might be an integer or a GUID.
routeData.Values["id"] = page.Id;
context.RouteData = routeData;
context.IsHandled = true;
return Task.FromResult(0);
public VirtualPathData GetVirtualPath(VirtualPathContext context)
VirtualPathData result = null;
PageInfo page = null;
// Get all of the pages from the cache.
var pages = GetPageList();
if (TryFindMatch(pages, context.Values, out page))
result = new VirtualPathData(this, page.VirtualPath);
context.IsBound = true;
return result;
private bool TryFindMatch(IEnumerable<PageInfo> pages, IDictionary<string, object> values, out PageInfo page)
page = null;
int id;
object idObj;
object controller;
object action;
if (!values.TryGetValue("id", out idObj))
return false;
id = Convert.ToInt32(idObj);
values.TryGetValue("controller", out controller);
values.TryGetValue("action", out action);
// The logic here should be the inverse of the logic in
// GetRouteData(). So, we match the same controller, action, and id.
// If we had additional route values there, we would take them all
// into consideration during this step.
if (action.Equals("Details") && controller.Equals("CustomPage"))
page = pages
.Where(x => x.Id.Equals(id))
.FirstOrDefault();
if (page != null)
return true;
return false;
private IEnumerable<PageInfo> GetPageList()
string key = "__CustomPageList";
IEnumerable<PageInfo> pages;
// Only allow one thread to poplate the data
if (!this.cache.TryGetValue(key, out pages))
lock (synclock)
if (!this.cache.TryGetValue(key, out pages))
// TODO: Retrieve the list of PageInfo objects from the database here.
pages = new List<PageInfo>()
new PageInfo() Id = 1, VirtualPath = "somecategory/somesubcategory/content1" ,
new PageInfo() Id = 2, VirtualPath = "somecategory/somesubcategory/content2" ,
new PageInfo() Id = 3, VirtualPath = "somecategory/somesubcategory/content3"
;
this.cache.Set(key, pages,
new MemoryCacheEntryOptions()
Priority = CacheItemPriority.NeverRemove,
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(15)
);
return pages;
CustomRoute DI 注册
services.AddTransient<ICustomRoute, CustomRoute>();
MVC 路由配置
// Add MVC to the request pipeline.
app.UseMvc(routes =>
routes.Routes.Add(routes.ServiceProvider.GetService<ICustomRoute>());
routes.MapRoute(
name: "default",
template: "controller=Home/action=Index/id?");
// Uncomment the following line to add a route for porting Web API 2 controllers.
// routes.MapWebApiRoute("DefaultApi", "api/controller/id?");
);
如果重要的话,我会使用 Beta 5
、DNX 4.5.1
和 DNX Core 5
。
解决方案
根据我在这里学到的信息,我创建了一个通用解决方案,可用于 URL 2 向映射in this answer 的简单主键。主键的控制器、动作、数据提供者和数据类型可以在将其连接到 MVC 6 路由时指定。
【问题讨论】:
【参考方案1】:正如@opants 所说,问题在于您在RouteAsync
方法中什么也没做。
如果您的意图是最终调用控制器操作方法,则可以使用以下方法而不是默认 MVC 路由:
默认情况下,MVC 使用
TemplateRoute
带有内部目标IRouter
。在 RouteAsync 中,TemplateRoute 将 委托给内部 IRouter。这个内部路由器被设置为MvcRouteHandler
默认情况下builder extensions。 在您的情况下,首先添加一个IRouter
作为您的内部目标:
public class CustomRoute : ICustomRoute
private readonly IMemoryCache cache;
private readonly IRouter target;
private object synclock = new object();
public CustomRoute(IMemoryCache cache, IRouter target)
this.cache = cache;
this.target = target;
然后更新您的启动以将该目标设置为MvcRouteHandler
,该目标已设置为routes.DefaultHandler
:
app.UseMvc(routes =>
routes.Routes.Add(
new CustomRoute(routes.ServiceProvider.GetRequiredService<IMemoryCache>(),
routes.DefaultHandler));
routes.MapRoute(
name: "default",
template: "controller=Home/action=Index/id?");
// Uncomment the following line to add a route for porting Web API 2 controllers.
// routes.MapWebApiRoute("DefaultApi", "api/controller/id?");
);
最后,更新您的 AsyncRoute 方法以调用内部 IRouter
,即 MvcRouteHandler
。您可以在TemplateRoute
中使用该方法的实现作为指导。我很快就使用了这种方法,并将您的方法修改如下:
public async Task RouteAsync(RouteContext context)
var requestPath = context.HttpContext.Request.Path.Value;
if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
// Trim the leading slash
requestPath = requestPath.Substring(1);
// Get the page that matches.
var page = GetPageList()
.Where(x => x.VirtualPath.Equals(requestPath))
.FirstOrDefault();
// If we got back a null value set, that means the URI did not match
if (page == null)
return;
//Invoke MVC controller/action
var oldRouteData = context.RouteData;
var newRouteData = new RouteData(oldRouteData);
newRouteData.Routers.Add(this.target);
// TODO: You might want to use the page object (from the database) to
// get both the controller and action, and possibly even an area.
// Alternatively, you could create a route for each table and hard-code
// this information.
newRouteData.Values["controller"] = "CustomPage";
newRouteData.Values["action"] = "Details";
// This will be the primary key of the database row.
// It might be an integer or a GUID.
newRouteData.Values["id"] = page.Id;
try
context.RouteData = newRouteData;
await this.target.RouteAsync(context);
finally
// Restore the original values to prevent polluting the route data.
if (!context.IsHandled)
context.RouteData = oldRouteData;
更新 RC2
看起来TemplateRoute
不再出现在 RC2 aspnet 路由中。
我调查了历史,并将其在 commit 36180ab 中重命名为 RouteBase
,作为更大重构的一部分。
【讨论】:
是的,我也想过拥有一个内部 IRouter,但我认为您不需要它。将 context.IsHandled 设置为 false 并提前返回将导致它移动到下一个注册的 IRouter 并最终回退到 routes.DefaultHandler(即 MvcRouteHandler) 如果没有匹配的路由,你确定使用 DefaultHandler 吗?查看代码,它似乎仅用于MapRoute
扩展方法,因此使用带有内部 MvcRouteHandler 的 TemplateRoute 添加 MVC 路由
还要检查RouteBuilder.Build
,它只会添加每个定义的路由,而不是默认处理程序
@DanielJ.G. - 这更有意义。我有点想知道为什么MvcRouteHandler
不像过去的版本那样是等式的一部分。更不用说,重新实现它已经做的所有事情似乎没有多大意义。我会试一试的。
对我来说,一开始这让我很困惑,因为 MvcRouteHandler 不是用来匹配传入的 url,而是用来处理它。但是,它实现了您在使用自定义路由逻辑编写自定义路由时需要实现的相同 IRouter
接口。所以基本上要实现您的自定义路由逻辑,但仍委托给 MVC 管道以服务请求,您必须创建一个 IRouter
,包装另一个 IRouter
,恰好是 MvcRouteHandler
... 不确定我是否解释过我! :)【参考方案2】:
这不起作用的主要原因是您没有在 RouteAsync
方法中做任何事情。另一个原因是 MVC 6 中路由的工作方式与以前的 MVC 路由的工作方式非常不同,因此您最好使用 source code 作为参考从头开始编写它,因为很少有文章解决 MVC 6瞬间。
编辑:@Daniel J.G.答案比这更有意义,所以尽可能使用它。这可能适合其他人的用例,所以我把它留在这里。
这是一个使用 beta7 的非常简单的IRouter
实现。这应该可行,但您可能需要填补空白。您需要删除 page != null
并将其替换为以下代码并替换控制器和操作:
if (page == null)
// Move to next router
return;
// TODO: Replace with correct controller
var controllerType = typeof(HomeController);
// TODO: Replace with correct action
var action = nameof(HomeController.Index);
// This is used to locate the razor view
// Remove the trailing "Controller" string
context.RouteData.Values["Controller"] = controllerType.Name.Substring(0, controllerType.Name.Length - 10);
var actionInvoker = context.HttpContext.RequestServices.GetRequiredService<IActionInvokerFactory>();
var descriptor = new ControllerActionDescriptor
Name = action,
MethodInfo = controllerType.GetTypeInfo().DeclaredMethods.Single(m => m.Name == action),
ControllerTypeInfo = controllerType.GetTypeInfo(),
// Setup filters
FilterDescriptors = new List<FilterDescriptor>(),
// Setup DI properties
BoundProperties = new List<ParameterDescriptor>(0),
// Setup action arguments
Parameters = new List<ParameterDescriptor>(0),
// Setup route constraints
RouteConstraints = new List<RouteDataActionConstraint>(0),
// This router will work fine without these props set
//ControllerName = "Home",
//DisplayName = "Home",
;
var accessor = context.HttpContext.RequestServices.GetRequiredService<IActionContextAccessor>();
accessor.ActionContext = new ActionContext(context.HttpContext, context.RouteData, descriptor);
var actionInvokerFactory = context.HttpContext.RequestServices.GetRequiredService<IActionInvokerFactory>();
var invoker = actionInvokerFactory.CreateInvoker(accessor.ActionContext);
// Render the page
await invoker.InvokeAsync();
// Don't execute the next IRouter
context.IsHandled = true;
return;
确保添加对 Microsoft.Framework.DependencyInjection
命名空间的引用以解析 GetRequiredService
扩展。
之后,按照以下方式注册 IRouter:
app.UseMvc(routes =>
// Run before any default IRouter implementation
// or use .Add to run after all the default IRouter implementations
routes.Routes.Insert(0, routes.ServiceProvider.GetRequiredService<CustomRoute>());
// .. more code here ...
);
然后在你的 IOC 中注册,
services.AddSingleton<CustomRoute>();
另一种“更清洁”的方法可能是创建 IActionSelector
的不同实现。
【讨论】:
关闭,但没有雪茄。在删除关于创建IActionContextAccessor
只是为了设置然后获取其属性之一的废话之后,我能够成功调用该方法。但是,现在我得到一个空引用异常。堆栈跟踪的前 2 行是:Microsoft.AspNet.Mvc.UrlHelper..ctor(IScopedInstance
1 contextAccessor, IActionSelector actionSelector)` 和 lambda_method(Closure , ServiceProvider )
。开始认为可能有一个错误。我打算从 Beta 5 升级到 Beta 7,看看我能不能让它工作,如果不能,报告给微软。
抱歉没有仔细阅读您的问题并假设您使用的是 beta 7。上面的代码在 beta 7 中进行了测试。很确定这在 beta 5 上不起作用。请参阅我关于添加它的编辑到 routes.Routes.Insert 也是如此。以上是关于在 ASP.NET 5 (vNext) MVC 6 中实现自定义路由器的主要内容,如果未能解决你的问题,请参考以下文章