【问题标题】:path_prefix for asp.net mvc routesasp.net mvc 路由的 path_prefix
【发布时间】:2009-04-29 16:40:45
【问题描述】:

我阅读了this 文章,了解如何在 ruby​​ on rails 中为路由添加前缀。我希望能够用 asp.net mvc 做同样的事情

所以我希望能够定义如下路线:

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

有什么建议吗?

编辑:

"photographers/{id}/photos/{photoID}" - 似乎做得很好,但是我该如何支持

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

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

【问题讨论】:

    标签: asp.net-mvc routing


    【解决方案1】:

    这样定义你的路线:

    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】:

      您可以使用regex routingwildcards 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) });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多