自定义 IRouteHandler 路由搞砸了 MVC 链接构建

发布于 2024-10-06 14:35:53 字数 629 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

御守 2024-10-13 14:35:54

我想出了一个办法来阻止所描述的行为。虽然这“解决”了问题,但它确实有点令人讨厌,我更喜欢一种更干净的方法来从虚拟路径构建逻辑中排除路由

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”,希望这不太可能。我说这很恶心。

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()
);

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.

夜灵血窟げ 2024-10-13 14:35:54

您可以使用 RouteLink 帮助器指定路由的名称:

<%: Html.RouteLink("link text", "Default", 
    new { action = "add", controller = "home" }) %>

You could specify the name of the route using the RouteLink helper:

<%: Html.RouteLink("link text", "Default", 
    new { action = "add", controller = "home" }) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文