未找到路线或路线构建不正确

发布于 2024-10-02 11:03:17 字数 1367 浏览 1 评论 0原文

以下是我在 global.asax.cs 中的路由定义:

routes.Add(
  new NamedTypedRoute(
    "feedback-en", RouteType.Regular, "{culture}/Feedback",
    new RouteValueDictionary(
      new
      {
        culture = "en",
        controller = "Feedback",
        action = "Index"
      }
    ),
    null,
    new MultiLingualMvcRouteHandler()
  )
);

routes.Add(
  new NamedTypedRoute(
    "feedback-sl", RouteType.Regular, "{culture}/Kontakt",
    new RouteValueDictionary(
      new
      {
        culture = "sl",
        controller = "Feedback",
        action = "Index"
      }
    ),
    null,
    new MultiLingualMvcRouteHandler()
  )
);

如果我在视图中执行此操作,则

<%: Html.ActionLink("sl", "feedback-sl")%> | <%: Html.ActionLink("en", "feedback-en")%>

构造的 URL 指向根站点(构造的链接中不包含控制器/操作信息)。

如果我在视图中执行此操作,

<%: Html.RouteLink("sl", "feedback-sl")%> | <%: Html.RouteLink("en", "feedback-en")%>

则会发生异常:

"A route named 'feedback-sl' could not be found in the route collection.
Parameter name: name"

我的两个问题:

  1. 为什么有两个非常相似的帮助器,RouteLink 和 ActionLink?他们之间有什么区别?
  2. 我想我的 NamedTypedRoute 实现可能有问题。我需要它具有命名路线和键入路线 - 路线可以是管理路线和常规路线。我使用此信息根据路由定义动态构建管理菜单。对于管理页面,我有指向资源字符串的名称,然后在管理页面标题中使用这些名称。这样我就有了可本地化的路线名称。我把这个复杂化了吗?

The following is my routes definition from global.asax.cs:

routes.Add(
  new NamedTypedRoute(
    "feedback-en", RouteType.Regular, "{culture}/Feedback",
    new RouteValueDictionary(
      new
      {
        culture = "en",
        controller = "Feedback",
        action = "Index"
      }
    ),
    null,
    new MultiLingualMvcRouteHandler()
  )
);

routes.Add(
  new NamedTypedRoute(
    "feedback-sl", RouteType.Regular, "{culture}/Kontakt",
    new RouteValueDictionary(
      new
      {
        culture = "sl",
        controller = "Feedback",
        action = "Index"
      }
    ),
    null,
    new MultiLingualMvcRouteHandler()
  )
);

If I do this in the view

<%: Html.ActionLink("sl", "feedback-sl")%> | <%: Html.ActionLink("en", "feedback-en")%>

the constructed URL points to the root site (no controller/action information is included in the constructed link).

If I do this in the view

<%: Html.RouteLink("sl", "feedback-sl")%> | <%: Html.RouteLink("en", "feedback-en")%>

an exception occurs:

"A route named 'feedback-sl' could not be found in the route collection.
Parameter name: name"

My two questions:

  1. Why are there two very similar helpers, RouteLink and ActionLink? What's the difference between them?
  2. I guess there could be something wrong with my NamedTypedRoute implementation. I need this to have named routes and typed routes - route can be admin and regular. I use this information to dynamically construct administration menu based on routes definition. For administration pages, I have names pointing to resource strings and then I use those names in administration page titles. That way I have localizable route names. Am I overcomplicating this ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

孤独岁月 2024-10-09 11:03:17

Html.ActionLink 扩展呈现链接到操作的锚元素。另一方面,Html.RouteLink 扩展呈现一个锚元素,该元素可以解析为操作方法、文件、文件夹或某些其他资源。 RouteLink 并不像 ActionLink 那样真正采用 ActionName 和 ControllerName 字符串。从更多细节来看一下参数的参数名称。这里的描述在 MSDN/IntelliSense 中写得不太好。

遗憾的是我没有第二个问题的答案。

Html.ActionLink extension renders an anchor element that links to an action. Html.RouteLink extension on the other hand renders an anchor element which could resolve to an action method, a file, a folder, or some other resource. The RouteLink doesn't really take ActionName and ControllerName strings like the ActionLink. From more detail look a bit at the parameter names for the parameters. The descriptions here are not really well written in MSDN/IntelliSense.

Sadly I don't have an answer for the second question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文