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

时间:2015-07-30 13:40:52

标签: asp.net-mvc mvcsitemapprovider asp.net-mvc-sitemap

我的自定义路线是:

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. 如何按如下所示显示节点。
  3. 网址: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

1 个答案:

答案 0 :(得分:2)

SiteMap是一种层次结构。 MvcSiteMapProvider的工作方式是匹配“当前”节点,然后使用层次结构构建返回主页的链接。它类似于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属性,但其当前实现不允许将标题设置为当前节点和父节点。所以你需要做这样的事情来设置每个节点的标题:

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之外还有另一个选项 - 您可以使用动态节点提供程序为每个类别,子类别,lowercategory等添加节点(并正确嵌套它们),然后每个节点都有自己的选项标题自动。有关示例,请参阅How To Make MvcSiteMapProvider Remember a User's Position