简体   繁体   English

.NET MVC2自定义子页面路由

[英].NET MVC2 custom subpage routes

In searching I came across ASP.NET MVC page/subpage routing and the wildcard was somewhat of a lifesaver, but presented a new problem. 在搜索过程中,我遇到了ASP.NET MVC页面/子页面路由 ,而通配符有点像救命稻草,但是却带来了一个新问题。

Consider the following routes, if you will. 如果可以,请考虑以下路线。 The goal here is to have the index ( http://www.domain.com ) use the default route and [domain]/page use the Content route. 这里的目标是使索引( http://www.domain.com )使用默认路由, [domain] / page使用内容路由。 The strSlug is a wildcard because I plan on splitting the slug to use for subpages. strSlug是一个通配符,因为我计划拆分该子段以用于子页面。 [domain]page/subpage (subpage would be the current slug). [domain]页面/子页面 (子页面将是当前子弹)。 With it being a wildcard, it seems like [domain] is matching on the Content route. 由于它是通配符,因此似乎[domain]在Content路由上匹配。

What would be the proper way around that? 解决该问题的正确方法是什么?

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

EDIT: In doing some more research, I was hoping to use something like this. 编辑:在做更多的研究时,我希望使用这样的东西。

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

..but this results in a redirect loop for some reason. ..但是由于某种原因,这会导致重定向循环。

EDIT #2 So this solution kind of, sort of, maybe works? 编辑#2因此,这种解决方案有点,也许可行? I have yet to see a downside. 我还没有看到不利的一面。

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

So [domain]/slug works.. and anything after that it parses off the last slug.. so [domain]slug/sub/sub/sub/content would grab the page matching the content slug. 因此,[domain] / slug可以工作..然后解析出最后一个slug ..因此,[domain] slug / sub / sub / sub / content会抓取与内容slug匹配的页面。

Putting the wild card as your first parameter in your url route will route every request to Content route. 将通配符作为url路由的第一个参数会将每个请求路由到Content路由。 To achieve what you want, you can use this route. 要实现您想要的,您可以使用此路线。

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

By accessing www.domain.com will use the default route and with www.domain.com/Page will use the Content route so as with www.domain.com/Page/subpage . 通过访问www.domain.com将使用默认的路由,并与www.domain.com/Page将使用Content的路由,以便与www.domain.com/Page/subpage。

www.domain.com/Page/subpage1/subpage2

The above url will give your strSlug parameter of your SiteContent ActionResult the value subpage1/subpage2 . 上面的url将为您的SiteContent ActionResult的strSlug参数提供值subpage1/subpage2 Then you can parse your strSlug to get your content. 然后,您可以解析strSlug以获得内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM