代码之家  ›  专栏  ›  技术社区  ›  Don Boots

.NET MVC2自定义子页路由

  •  0
  • Don Boots  · 技术社区  · 16 年前

    在寻找中我遇到了 ASP.NET MVC page/subpage routing 通配符在某种程度上是一个救星,但也带来了一个新的问题。

    如果你愿意的话,可以考虑下面的路线。这里的目标是建立索引( http://www.domain.com )使用默认路线和 [域]/页 使用 内容 路线。strSlug是一个通配符,因为我计划拆分slug以用于子页面。 (子页面将是当前的slug)。因为它是一个通配符,所以看起来[domain]与

    怎样才能解决这个问题?

    // Global.asx.cs  
    routes.MapRoute(
            "Content", // Route name
            "{*strSlug}", // URL with parameters
            new { controller = "Home", action = "SiteContent", strSlug = UrlParameter.Optional } // Parameter defaults
        );
    
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}", // URL with parameters
            new { controller = "Home", action = "Index" } // Parameter defaults
        );
    
        // HomeController.cs
        // Not the best code, feel free to improve.
        public ActionResult SiteContent(string strSlug)
        {
            string[] strSlug_arr = strSlug.Split("/".ToCharArray());
            strSlug = strSlug_arr[(strSlug_arr.Length-1)];
    
            SiteContent data = DB.SiteContents.Single(c => c.Slug == strSlug);
            return View(data);
        }
    

    编辑: 在做更多的研究时,我希望用这样的方法。

    public ActionResult Index()
    {
        SiteContent data = DB.SiteContents.Single(c => c.Slug == "home-page");
        return View(data);
    }
    
    public ActionResult SiteContent(string strSlug)
    {
        if (strSlug == null)
        {
            return RedirectToAction("Index");
        }
        else
        {
            string[] strSlug_arr = strSlug.Split("/".ToCharArray());
            strSlug = strSlug_arr[(strSlug_arr.Length - 1)];
    
            SiteContent data = DB.SiteContents.Single(c => c.Slug == strSlug);
            return View(data);
        }
    }
    

    ..但由于某些原因,这会导致重定向循环。

    编辑#2 所以这个解决方案,有点,可能有用?我还没有看到不利的一面。

        // Global.asx.cs
        routes.MapRoute(
            "Content", // Route name
            "{strSlug}/{*strSubSlug}", // URL with parameters
            new { controller = "Home", action = "SiteContent", strSubSlug = UrlParameter.Optional } // Parameter defaults
        );
    
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/", // URL with parameters
            new { controller = "Home", action = "Index" } // Parameter defaults
        );
    
    
        // HomeController.cs
        public ActionResult Index()
        {
            SiteContent data = DB.SiteContents.Single(c => c.Slug == "home-page");
            return View(data);
        }
    
        public ActionResult SiteContent(string strSlug, string strSubSlug)
        {
            /*
            string[] strSlug_arr = strSlug.Split("/".ToCharArray());
            strSlug = strSlug_arr[(strSlug_arr.Length - 1)];
             */
    
            if (strSubSlug != null)
            {
                string[] strSubSlug_arr = strSubSlug.Split("/".ToCharArray());
                strSlug = strSubSlug_arr[(strSubSlug_arr.Length - 1)];
            }
    
            SiteContent data = DB.SiteContents.Single(c => c.Slug == strSlug);
            return View(data);
        }
    

    1 回复  |  直到 9 年前
        1
  •  1
  •   rob waminal    16 年前

    将通配符作为url路由的第一个参数将把每个请求路由到 Content

    route.MapRoute(
        "Content",
        "Page/{*strSlug}",
        new { controller = "Home", action = "SiteContent", strSlug = UrlParameter.Optional }
    );
    

    通过访问 将使用默认路由和 www.domain.com/Page 将使用 以…为路线 www.domain.com/Page/subpage

    www.domain.com/Page/subpage1/subpage2
    

    以上网址将为您提供 strSlug SiteContent ActionResult值 subpage1/subpage2 . 然后你可以解析你的