如何找到一个RouteCollectionRoute正确的路线? [英] How to find the right route in a RouteCollectionRoute?

查看:226
本文介绍了如何找到一个RouteCollectionRoute正确的路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试ASP MVC路线的。我有问题与ASP MVC 5.1

当我有这样一个控制器:

When I have a controller like this:

public class FooController : Controller
{
    [Route("foo")]
    [HttpGet]
    public ActionResult Get()
    {
      .... 
    }

    [Route("foo")]
    [HttpPost]
    public ActionResult Post()
    {
      .... 
    }
}

然后,为了测试哪个路径特定请求匹配,我称之为<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.getroutedata%28v=vs.110%29.aspx\"><$c$c>routes.GetRouteData.我收到了 System.Web.Routing.RouteData 包含路线以及价值观应该说哪些控制器和动作相匹配。

Then in order to test which route matches a particular request, I call routes.GetRouteData. I get a System.Web.Routing.RouteData that contains the route as well as values that should say which controller and action are matched.

的问题是,这条路线是现在的<一个实例href=\"http://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Http/Routing/RouteCollectionRoute.cs\">RouteCollectionRoute
这个内部类是在一组路由到这两个动作的包装。该控制器是在 RouteData.values​​ ,但动作不大。你可以得到这些载带路线routeData.Values​​ [MS_DirectRouteMatches]作为IList的&LT;&的RouteData GT;

The problem is that this route is now an instance of RouteCollectionRoute this internal class is a wrapper over a group of routes to these two actions. The controller is in the RouteData.values, but the action is not. You can get as these contained routes with routeData.Values["MS_DirectRouteMatches"] as IList<RouteData>.

在给出的例子中,会出现在列表分两路,对于两个操作的方法。我需要知道其中包含的航线实际上是匹配的,这样我就可以读出的路由条目的操作方法的名称是什么。如何解决这条路?

In the example given, there will be two routes in the list, for the two action methods. I need to know which of the contained routes is actually matched, so that I can read off what the matched route's action method name is. How do I resolve this route?

我有点被设计的选择都具有 RouteCollectionRoute 的疑惑。
当路由,你有一个路由集合,然后通过调用 GetRouteData 解析的URL路径。现在你有一个路由。但是,如果它是一个 RouteCollectionRoute 所以它的还是路线和路由解析集合尚未结束。什么是被具有RouteCollectionRoute得到了什么?是路由解析现在两个步骤?或一个递归的过程?

I'm a bit puzzled by the design choice of having a RouteCollectionRoute at all. When routing, you have a route collection, then you resolve the route for a url by calling GetRouteData. Now you have one route. But if it's a RouteCollectionRoute so it's still a collection of routes and route resolution isn't over yet. What's been gained by having a RouteCollectionRoute? Is route resolution now a two-step process? Or a recursive process?

我知道我通过ASP MVC的内部拖网,但它是因为它确实没有设计这种测试性的考虑的唯一途径。即使是简单的事情,如暴露会帮助很多一些内部类或方法!

I know I am trawling through the internals of ASP MVC, but it's the only way as it really wasn't designed with this kind of testability in mind. Even simple things like exposing some internal classes or methods would have helped a lot!

推荐答案

属性的路由在ASP.NET MVC比基于传统惯例有点棘手。您可以从 System.Web.Mvc.Controller.ExecuteCore开始审查(),其中 GetActionName()的属性返回null路由和让动作选择器选择执行何种操作。然后你就可以跳转到 System.Web.Mvc.ControllerActionInvoker.FindAction()做所有的魔法,并发现RouteCandidates然后ActionNameSelectors过滤它们,ActionSelectors等。

Attribute routing is a bit trickier in ASP.NET MVC than traditional convention based. You can start review from System.Web.Mvc.Controller.ExecuteCore() where GetActionName() returns null for attribute routing and let action selector choose which action to execute. Then you can jump to System.Web.Mvc.ControllerActionInvoker.FindAction() that does all magic and finds RouteCandidates then filters them by ActionNameSelectors, ActionSelectors etc.

我做了与实施拉申请换行ASP.NET MVC逻辑

I've made a pull request with implementation that wraps ASP.NET MVC logic.

一个重要的事情补充:
实现是一个有很多反射颇重。请看看ASP.NET开发团队测试如何路由<一个href=\"http://aspnetwebstack.$c$cplex.com/SourceControl/latest#test/System.Web.Mvc.Test/Routing/AttributeRoutingTest.cs\"相对=nofollow>例如。在AttributeRoutingTest.cs 的。你可以用你自己的ControllerFactory将返回嘲笑控制器动作返回自己的名字,参数等。这将是我认为要容易得多。

One important thing to add: The implementation is quite heavy with a lot of reflection. Please take a look at how the ASP.NET team tests routing e.g. in AttributeRoutingTest.cs. You can use you own ControllerFactory that will return mocked controller with actions returning their names, parameters, etc. This would be much easier in my opinion.

这篇关于如何找到一个RouteCollectionRoute正确的路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆