代码之家  ›  专栏  ›  技术社区  ›  Lareau

站点地图/面包屑在默认页面上不显示Url路由

  •  3
  • Lareau  · 技术社区  · 14 年前

    我已经用UrlRouting设置了我的web表单站点(4.0)。

    我的主要问题是 http://Localhost/

    因为它违约了 http://Localhost/default.aspx

    我试图避免在sitemap xml中添加另一个元素

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

    最好的方法是什么?

    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;
    
        }
    

    1 回复  |  直到 14 年前
        1
  •  2
  •   War    14 年前

    而不是:

    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,您基本上希望它们是相同的,因此您必须做出决定,并决定将哪个url重定向到另一个url。

    就我个人而言,如果这是我的解决方案,我将没有“~/Home”的路径,并且我的站点地图中的基本节点将如下所示:

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

    很明显,“http://yourdomain/”是主页,“http://yourdomain/Home”可以是任何东西(关于你的家、我的家、温馨的家、我喜欢的家),而“http://adomain/”是全球所有人的主页。

    推荐文章