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

在ASP.NET MVC中,如何允许URL后面有额外的文本?

  •  1
  • shsteimer  · 技术社区  · 16 年前

    我的应用程序中有这条唯一的路线:

    routes.MapRoute(
        "Default",                            
        "{controller}/{action}/{id}",         
        new { controller = "Home", action = "Index", id = ""} 
    );
    

    这对以下网址很有用:

    /BLAH/指数
    /创造/创造
    /细节/细节5

    我想给最后一个添加文本,就像这样:

    /blah/details/5/page title here或随便什么

    所以我的问题是:

    我的路线应该是什么样的? (或者如果它与路线没有任何关系……我该怎么做?)

    1 回复  |  直到 16 年前
        1
  •  2
  •   No Refunds No Returns    16 年前

    MSDN链路: http://msdn.microsoft.com/en-us/library/cc668201.aspx

    routes.MapRoute( 
        "Default",                             
        "{controller}/{action}/{id}/{*allTheRest}",          
        new { controller = "Home", action = "Index", id = "", allTheRest=""}  
    ); 
    

    函数签名应类似于

        public ActionResult MyAction(int? id, string rest)
        {
            this.TempData["ID"] = id ?? -1000;
            this.TempData["REST"] = rest ?? "Not Provided";
            return View();
        }
    
    推荐文章