未找到路线或路线构建不正确
以下是我在 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"
我的两个问题:
- 为什么有两个非常相似的帮助器,RouteLink 和 ActionLink?他们之间有什么区别?
- 我想我的 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:
- Why are there two very similar helpers, RouteLink and ActionLink? What's the difference between them?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.