1

我的自定义路线是:

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显示节点。为什么?
  2. 如何显示节点,如下所述。

网址:http://localhost:59328/Home/Index/mobiles

预期的站点地图:Home > mobiles

网址:http://localhost:59328/Home/Index/mobiles/htc

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

网址:http://localhost:59328/Home/Index/mobiles/htc/m8

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

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

但实际的站点地图总是Home

4

1 回答 1

2

SiteMap 是一个层次结构。可行的方法MvcSiteMapProvider是匹配“当前”节点,然后使用层次结构构建返回主页的链接。它类似于ASP.NET中的Microsoft 站点地图提供程序。

要获得所需的层次结构,您需要像使用 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属性,但其当前实现不允许将标题设置为超过当前节点和父节点。所以你需要做这样的事情来设置每个节点的标题:

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- 您可以使用动态节点提供程序为每个类别、子类别、较低类别等添加一个节点(并正确嵌套它们),然后每个节点将自动拥有自己的标题。有关示例,请参阅如何使 MvcSiteMapProvider 记住用户的位置

于 2015-08-02T13:01:12.770 回答