自定义IRouteHandler路由搞砸了MVC链接的建立 [英] Custom IRouteHandler route screws up MVC link building
问题描述
我在ASP.NET MVC2中有一个自定义路由处理程序,可以在这样的前缀路径中捕获所有url:
I have a custom routehandler in ASP.NET MVC2 to catch all url's at a prefixed path like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new Route("@api/{*all}", new ApiHandler()));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
路由工作正常,但是如果我使用Html.Actionlink或从控制器返回ReturnToAction(),则构建的uri将创建一个损坏的uri,如下所示:
Routing works fine, but if i use Html.Actionlink or return ReturnToAction() from a controller, the uri built creates a broken uri like this:
/@api?action=Add&controller=Home
代替
/Home/Add
我如何影响uri构建逻辑以考虑默认路由模式?
How can i influence the uri building logic to consider the Default route pattern?
推荐答案
我想出了一种方法来阻止所描述的行为.虽然这可以解决"问题,但确实有点烦人,我更喜欢一种更干净的方法来从虚拟路径构建逻辑中排除路由
I've come up with a hack to stop the described behavior. While this "solves" the problem, it's really kind of nasty and I'd prefer a cleaner way to exclude a route from the virtual path building logic
routes.Add(
new Route("@api/{*all}",
// A random and unlikely controller name as the default
new RouteValueDictionary() { { "controller", "qwewqewqeqweq" } },
// and a constraint requiring any controller to be that random, default value
new RouteValueDictionary() { { "controller", "qwewqewqeqweq" } },
new ApiHandler()
);
这意味着仅在所讨论的控制器为"qwewqewqeqweq"的情况下才选择用于虚拟路径构建的路由,希望这不太可能.我说这很讨厌.
This means that the route would only be chosen for Virtual Path building if the controller in question was "qwewqewqeqweq", which hopefully is unlikely. I said it was nasty.
这篇关于自定义IRouteHandler路由搞砸了MVC链接的建立的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!