MVCSiteMapProvider面包不正确的父节点ID [英] MVCSiteMapProvider breadcrumbs incorrect parent node id

查看:188
本文介绍了MVCSiteMapProvider面包不正确的父节点ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的网站地图:

<mvcSiteMapNode title="Projects" controller="Projects" action="Index" key="Home" visibility="!*">
<mvcSiteMapNode title="Projects" controller="Projects" action="Index">
  <mvcSiteMapNode title="Project" controller="Projects" action="Details" preservedRouteParameters="id">
    <mvcSiteMapNode title="Session" controller="Sessions" action="Details" preservedRouteParameters="id">
      <mvcSiteMapNode title="Edit Session" controller="Sessions" action="Edit" preservedRouteParameters="id"/>
    </mvcSiteMapNode>
  </mvcSiteMapNode>
</mvcSiteMapNode>
<mvcSiteMapNode title="My Account" controller="Account" action="ChangePassword" />
<mvcSiteMapNode title="Admin" controller="Admin" action="Index" >
  <mvcSiteMapNode title="Create User" controller="Admin" action="AddUser" />
  <mvcSiteMapNode title="Manage Users" controller="Admin" action="Users" />
</mvcSiteMapNode>

当我去会议详细信息页,面包屑显示:

When I go to Session Details page, the breadcrumbs display:

Projects > Project > Session

不过该项目链接,进入项目详情页面中,使用的是相同的ID会话,而不是它来自该项目。

however the project link, which goes to the Project Details page, is using the same id as the session, not the project it came from.

我尝试添加 inheritedRouteParameters =ID来会话详细信息页面,但它并没有改变任何东西。

I tried adding inheritedRouteParameters="id" to the Session Details page, but it didn't change anything.

编辑:
我加了不同的preservedRouteParameters,但现在项目链接回/项目/无ID详细连接。

I added different preservedRouteParameters, but now "Project" links back to /Projects/Details without the id attached.

推荐答案

preservedRouteParameters始终复制当前请求如果密钥名称相匹配,这都没有什么标识是假设值。因此,如果在被同时显示2个节点preservedRouteParameters使用身份证,你需要确保它们具有相同的含义。

preservedRouteParameters always copies the value from the current request if the key name matches, it makes no assumptions about what the "id" is. Therefore, if you use "id" in preservedRouteParameters on 2 nodes that are displayed at the same time, you need to make sure they have the same meaning.

围绕这一点的一种方法是使用不同的密钥名称为每一种情况下(专案编号和的sessionId,例如)。然后,你可以preserve俩孩子节点所以它会记住它属于什么样的父母。

One way around this is to use a different key name for each case ("projectId" and "sessionId", for example). Then you can preserve both on the child node so it will "remember" what parent it belongs to.

<mvcSiteMapNode title="Project" controller="Projects" action="Details" preservedRouteParameters="projectId">
    <mvcSiteMapNode title="Session" controller="Sessions" action="Details" preservedRouteParameters="projectId,sessionId">

您可能需要修改你的路由让本作可以接受你的要求的网址,但这个工作父ID必须是子节点的路径(通常为URL)的一部分。下面是上述节点配置相匹配,例如,

You may need to modify your routes to get this to make URLs that are acceptable to your requirements, but for this to work the parent ID must be a part of the route (and usually the URL) of the child node. Here is an example that matches the above node configuration.

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "SessionRoute", 
            url: "Project/{projectId}/{sessionId}", 
            defaults: new { controller = "Sessions", action = "Details" });

        routes.MapRoute(
            name: "ProjectRoute", 
            url: "Project/{projectId}", 
            defaults: new { controller = "Projects", action = "Details" });

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

有一个看MvcSiteMapProvider-强制-A-比赛-2级别项目的在这个演示,看看究竟如何可以做到这一点。请注意添加路由是可选的 - 你总是可以使用默认路由,如果你不介意在URL查询字符串参数

Have a look at the MvcSiteMapProvider-Forcing-A-Match-2-Levels project in this demo to see exactly how this can be done. Do note that adding the routes is optional - you can always use the default route if you don't mind querystring parameters in the URL.

如果不适合你,那么你可以使用preservedRouteParmeters上的一个节点参数结合了明确设置其他参数。

If that doesn't suit you, then you can combine using preservedRouteParmeters on one node parameter with setting another parameter explicitly.

<mvcSiteMapNode title="Project" controller="Projects" action="Details" preservedRouteParameters="projectId">
    <mvcSiteMapNode title="Session" controller="Sessions" action="Details" id="1" preservedRouteParameters="projectId">
    <mvcSiteMapNode title="Session" controller="Sessions" action="Details" id="2" preservedRouteParameters="projectId">
    <mvcSiteMapNode title="Session" controller="Sessions" action="Details" id="3" preservedRouteParameters="projectId">

或者你也可以为IDS,你必须每个组合创建一个节点。

Or you can create a node for each combination of "id"s that you have.

<mvcSiteMapNode title="Project" controller="Projects" action="Details" id="1">
    <mvcSiteMapNode title="Session" controller="Sessions" action="Details" id="1">
    <mvcSiteMapNode title="Session" controller="Sessions" action="Details" id="2">
    <mvcSiteMapNode title="Session" controller="Sessions" action="Details" id="3">
</mvcSiteMapNode>
<mvcSiteMapNode title="Project" controller="Projects" action="Details" id="2">
    <mvcSiteMapNode title="Session" controller="Sessions" action="Details" id="4">
    <mvcSiteMapNode title="Session" controller="Sessions" action="Details" id="5">
    <mvcSiteMapNode title="Session" controller="Sessions" action="Details" id="6">
</mvcSiteMapNode>

有关这些过去2个选择,通常最好使用<一个href=\"https://github.com/maartenba/MvcSiteMapProvider/wiki/Defining-sitemap-nodes-using-IDynamicNodeProvider\"相对=nofollow> DynamicNodeProvider 来填充动态数据,而不是XML标记他们都放弃了。

For these last 2 options, it is usually best to use a DynamicNodeProvider to populate the dynamic data rather than marking them all up in XML.

这些方法创造更清洁的URL,而且还使用了更多的RAM。一般对于管理页面,不如一起去稍微马虎的URL(preservedRouteParmaters)并保存使用动态节点(和RAM)为需要搜索引擎索引的网页。

These methods create cleaner URLs, but also use up more RAM. Generally for administration pages, it is better to go with the slightly sloppy URLs (preservedRouteParmaters) and save using dynamic nodes (and RAM) for pages that require search engine indexing.

inheritedRouteParameters仅用于从XML配置父节点继承值,并在其要求的水平没有影响。

inheritedRouteParameters is only for inheriting values from the parent node in the XML configuration and has no effect at the request level.

顺便说一句 - 你必须确保你有路由值的每个节点上的独特结合。在你的榜样,第一个2个节点具有完全相同的路由值,这意味着第二个永远不会被匹配,因为第一场比赛总是获胜。

BTW - You must ensure you have a unique combination of route values on each node. In your example, the first 2 nodes have exactly the same route values, meaning the second one will never be matched because the first match always wins.

这篇关于MVCSiteMapProvider面包不正确的父节点ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆