Sitemap / 面包屑导航在默认页面上使用Url路由未显示

3

我已经使用UrlRouting设置了我的Web表单站点(4.0)。 当我访问以下链接时,我的面包屑会出现

我的主要问题在于 http://Localhost/

因为它在IIS中默认为http://Localhost/default.aspx

我试图避免向站点地图xml添加另一个元素,例如:

<siteMapNode url="~/Home" title="Home"  description="Home" aspx="default.aspx">

使用什么方法最好?

我尝试将其添加到我的路由表中,并使用xmlSiteMapProvider来查看是否可以对其进行一些操作(但没有成功)。

routes.MapPageRoute("IISDefault", "", "~/Default.aspx");

这里有一些信息。

Routes
routes.MapPageRoute("Default", "Home", "~/Default.aspx");
routes.MapPageRoute("ListAll", "List", "~/ListAll.aspx");

Sitemap
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <siteMapNode url="~/Home" title="Home"  description="Home">
        <siteMapNode url="~/List" title="List All"  description="List All"  />
    </siteMapNode>
</siteMap>

XmlSiteMapProvider

   /// <summary>
    /// This is where the original sitemap node is overloaded.  We get the proper translation from the database.
    /// </summary>
    /// <param name="sender">This is the sender of the event</param>
    /// <param name="e">This is the event arguments</param>
    /// <returns>Returns a modified SiteMapNode</returns>
    /// <remarks></remarks>
    public SiteMapNode SmartSiteMapProvider_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
    {

        SiteMapNode returnValue = null;

        if ((SiteMap.CurrentNode == null))
        {
            // If we don't find a sitemap node, then we might be working with UrlRouting
            returnValue = ProcessRoute(e);
        }


        return returnValue;

    }

    private SiteMapNode ProcessRoute(SiteMapResolveEventArgs e)
    {

        SiteMapNode returnValue = null;

        System.Web.Routing.RequestContext rc = HttpContext.Current.Request.RequestContext;

        if ((rc != null))
        {
            System.Web.Routing.RouteBase route = rc.RouteData.Route;

            if ((route != null))
            {
              // Play with the node (Never getting here)
            }
        }

        return returnValue;

    }

编辑:我将尝试操纵routeCollection以某种方式进行匹配。

1个回答

2

代替:

Routes
routes.MapPageRoute("Default", "Home", "~/Default.aspx");
routes.MapPageRoute("ListAll", "List", "~/ListAll.aspx");

Sitemap
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <siteMapNode url="~/Home" title="Home"  description="Home">
        <siteMapNode url="~/List" title="List All"  description="List All"  />
    </siteMapNode>
</siteMap>

试试这个:

Routes
routes.MapPageRoute("Default", "Home", "~/");
routes.MapPageRoute("ListAll", "List", "~/ListAll.aspx");

Sitemap
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <siteMapNode url="~/" title="Home"  description="Home">
        <siteMapNode url="~/List" title="List All"  description="List All"  />
    </siteMapNode>
</siteMap>

否则,“~/”和“~/Home”是同一件事。
或者你可以保留上面的内容,在default.aspx页面做如下操作...
if(Page.RouteData.Values[0] == "default.aspx")
    Response.Redirect("~/Home")

这将有效地将任何默认请求重定向到您的默认请求。

您的问题在于服务器将~ /“和~ / Home”视为两个不同的URL,而您基本上希望它们是相同的,因此您必须做出决定并决定将哪一个重定向到另一个。

如果这是我的解决方案,我个人不会为“~ / Home”设置路由,我的站点地图中的基本节点将如下所示:

<siteMapNode url="~/" title="Home"  description="Home">

很明显,“http://yourdomain/”是主页,“http://yourdomain/Home”可以是任何东西(关于你的家,我的家,甜蜜的家园,我在家里喜欢的事物),而“http://adomain/”则是全球所有人的主页。


谢谢你的回答,Wardy。我需要重新开始编写这段代码。已经有一段时间了。我尝试将你的url="~/" title="home"放入我的网站地图中,但没有成功。当我访问Http://yourdomain时,面包屑并没有出现。 - Lareau
你期望在面包屑导航中看到什么?对于主页来说,从技术上讲,没有面包屑导航,因为面包屑导航显示的是从当前位置返回根目录的路径,因此主页是一种例外。如果我觉得相关,我倾向于硬编码链接到~/。问题是,如果你在主页上,为什么要显示一个指向主页的链接呢? - War
好观点,我一直以为如果你在主页上,它会只显示“主页”但没有链接。从我的快速谷歌搜索来看,人们只是从主页上删除它。谢谢这个技巧。 - Lareau
有些人确实喜欢这样做,我非常喜欢“不要链接到此页面”,但如果您在嵌套页面上并且使嵌套页面不链接到任何内容,则显示“主页>页面>嵌套页面”是一种很酷的方式。当您在主页上时,主页是面包屑导航的例外,因为没有逻辑路径。 - War

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接