使用动作参数作为 mvcsitemap 中的节点

using action parameters as nodes in mvcsitemap

我的自定义路线是:

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

Mvc.sitemap 是:

<mvcSiteMapNode title="Home" controller="Home" action="Index" preservedRouteParameters="category,subcategory,lowcategory,id,ignore">
    <mvcSiteMapNode title="About" controller="Home" action="About"/> 

我的Index函数是:

public ActionResult Index(string category, string subcategory, string lowcategory, int? id)
  1. preservedRouteParameters 没有显示 node.Why?
  2. 如何如下所述显示节点。

Url : http://localhost:59328/Home/Index/mobiles

预期站点地图:Home > mobiles

Url: http://localhost:59328/Home/Index/mobiles/htc

预期站点地图:Home > mobiles > htc

Url: http://localhost:59328/Home/Index/mobiles/htc/m8

预期站点地图:Home > mobiles > htc > m8

Url: http://localhost:59328/Home/Index/mobiles/htc/m8/12/title
预期站点地图:Home > mobiles > htc > m8 > title(注意 id 不包含在站点地图中)

但实际站点地图总是 Home

SiteMap 是一个层次结构。 MvcSiteMapProvider 的工作方式是匹配 "current" 节点,然后使用层次结构构建返回主页的链接。它类似于ASP.NET中的Microsoft sitemap provider

要获得所需的层次结构,您需要像使用 Windows 文件夹一样嵌套节点。

<mvcSiteMapNode title="Home" controller="Home" action="Index">
    <mvcSiteMapNode title="Category" controller="Home" action="Index" preservedRouteParameters="category" key="category">
        <mvcSiteMapNode title="Subcategory" controller="Home" action="Index"  preservedRouteParameters="category,subcategory" key="subcategory">
            <mvcSiteMapNode title="Lower Category" controller="Home" action="Index"  preservedRouteParameters="category,subcategory,lowercategory" key="lowercategory">
                <mvcSiteMapNode title="Title" controller="Home" action="Index"  preservedRouteParameters="category,subcategory,lowercategory,id,ignore" key="titleid"/>
            </mvcSiteMapNode>
        </mvcSiteMapNode>
    </mvcSiteMapNode>
    <mvcSiteMapNode title="About" controller="Home" action="About"/>
</mvcSiteMapNode>

此外,在一条路线中不能使用多个可选航段。您需要使用所需的段来构建路由。

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

routes.MapRoute(
    name: "LowerCategory",
    url: "Home/Index/{category}/{subcategory}/{lowercategory}",
    defaults: new { controller = "Home", action = "Index" }
);

routes.MapRoute(
    name: "Subcategory",
    url: "Home/Index/{category}/{subcategory}",
    defaults: new { controller = "Home", action = "Index" }
);

routes.MapRoute(
    name: "Category",
    url: "Home/Index/{category}",
    defaults: new { controller = "Home", action = "Index" }
);

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

最后,当使用 preservedRouteParameters 时,您需要做一些更多的工作,以便标题根据 URL 动态变化。有一个用于此目的的 SiteMapTitle 属性,但其当前实现不允许设置超过当前节点和 parent 节点的标题。所以你需要做这样的事情来设置每个节点的标题:

using MvcSiteMapProvider.Web.Mvc;

public class HomeController : Controller
{
    public ActionResult Index(
        string category, 
        string subcategory, 
        string lowercategory, 
        int id = 0, 
        string ignore = "")
    {

        var currentNode = this.GetCurrentSiteMapNode();
        if (currentNode != null)
        {
            switch (currentNode.Key)
            {
                case "titleid":
                    currentNode.Title = ignore;
                    currentNode.ParentNode.Title = lowercategory;
                    currentNode.ParentNode.ParentNode.Title = subcategory;
                    currentNode.ParentNode.ParentNode.ParentNode.Title = category;
                    break;
                case "lowercategory":
                    currentNode.Title = lowercategory;
                    currentNode.ParentNode.Title = subcategory;
                    currentNode.ParentNode.ParentNode.Title = category;
                    break;
                case "subcategory":
                    currentNode.Title = subcategory;
                    currentNode.ParentNode.Title = category;
                    break;
                case "category":
                    currentNode.Title = category;
                    break;
            }
        }

        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your app description page.";

        return View();
    }
}

请注意,除了 preservedRouteParameters 之外,还有另一个选项 - 您可以使用动态节点提供程序为每个类别、子类别、下级类别等添加一个节点(并正确嵌套它们),然后每个节点将具有它自己的标题自动。有关示例,请参阅 How To Make MvcSiteMapProvider Remember a User's Position