【问题标题】:using action parameters as nodes in mvcsitemap使用动作参数作为 mvcsitemap 中的节点
【发布时间】:2015-10-21 21:46:39
【问题描述】:

我的自定义路线是:

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 &gt; mobiles

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

预期站点地图:Home &gt; mobiles &gt; htc

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

预期站点地图:Home &gt; mobiles &gt; htc &gt; m8

网址:http://localhost:59328/Home/Index/mobiles/htc/m8/12/title
预期站点地图:Home &gt; mobiles &gt; htc &gt; m8 &gt; title(站点地图中不包含备注 ID)

但实际站点地图总是Home

【问题讨论】:

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


    【解决方案1】:

    站点地图是一个层次结构。 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,还有另一个选项 - 您可以使用动态节点提供程序为每个类别、子类别、下类别等添加一个节点(并正确嵌套它们),然后每个节点将自动拥有自己的标题.示例见How To Make MvcSiteMapProvider Remember a User's Position

    【讨论】:

    • 感谢您的精彩解释!
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 2013-08-22
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多