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

asp.net mvc路由的路径前缀

  •  0
  • sirrocco  · 技术社区  · 15 年前

    this 关于如何在RubyonRails中为路由添加前缀的文章。我希望能够用asp.net mvc做同样的事情

    因此,我希望能够定义一条路线,如:

    /photographers/1/photos/2   //photo 2 of photographer with id 1
    /photographers/1/photos     //all of photographer with id 1
    

    有什么建议吗?

    编辑:

    “摄影师/{id}/photos/{photoID}”

    RedirectToAction<PhotosController>(x => x.Add());
    

    我想重定向到:/photomers/1/photos/add

    2 回复  |  直到 15 年前
        1
  •  3
  •   Jon Benedicto    15 年前

    routes.MapRoute(
        "Photographers",
        "photographers/{id}/photos/{photoID}",
        new { controller = "Photographers", action = "Photo", photoID = null });
    

    然后定义控制器操作,如下所示:

    public ActionResult Photo(int id, int? photoID)
    {
        // If photoID is not null, show just that photo.
        // Otherwise, show all photographs.
    }
    
        2
  •  0
  •   hometoast    15 年前

    regex routing wildcards in your routing table 因此{id*}与 /1/photos/2 对于摄影师默认控制器,解析字符串,并重定向到适当的操作。

    也来看看 this

    RouteTable.Routes.Add(
            new Route { Url = "events/[eventId]/tickets/[action]/[id]", 
                        Defaults = new { controller = "Tickets", 
                                         action = "List", id = (string)null }, 
                        RouteHandler = typeof(MvcRouteHandler) });