自定义 IRouteHandler 路由搞砸了 MVC 链接构建
我在 ASP.NET MVC2 中有一个自定义路由处理程序,用于捕获前缀路径中的所有 url,如下所示:
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,如下所示:
/@api?action=Add&controller=Home
而不是
/Home/Add
如何影响 uri 构建逻辑来考虑默认路由模式?
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
);
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
instead of
/Home/Add
How can i influence the uri building logic to consider the Default route pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想出了一个办法来阻止所描述的行为。虽然这“解决”了问题,但它确实有点令人讨厌,我更喜欢一种更干净的方法来从虚拟路径构建逻辑中排除路由
这意味着只有当有问题的控制器是时才会选择该路由用于虚拟路径构建“qwewqewqeqweq”,希望这不太可能。我说这很恶心。
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
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.
您可以使用
RouteLink
帮助器指定路由的名称:You could specify the name of the route using the
RouteLink
helper: