How to find the right route in a RouteCollectionRoute?

馋奶兔 提交于 2019-11-30 18:16:30

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.

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

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.

@Anthony, if you only want to see which routes are followed with any given URL, then load the Cobisi Routing Assistant extension.

In the Visual Studio IDE, go to Tools->Extension Manager.... Then, make sure you have the Online Gallery tab selected and type "cobisi" in the search box. Once you have installed the extension, you will get a new item in the Tools menu called Cobisi (natch).

Choose the URL Mapper, unlock the project if it complains about it being locked, and then enter a URL. When you click on the Map button, you will see how the URL is routed. Super awesome if you are stuck with routing issues, IMHO.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!