2010-01-16 94 views
1

这之间的静态字符串是我的路由:ASP.NET MVC路由两个参数与

routes.MapRoute(null, 
    "shelves/{id1}/products/{action}/{id2}", 
    new { controller = "Products", action = "List", id1 = "", id2 = ""}); 

思想是,你可以做这样的事情:

http://server/shelves/23/products/edit/14 

而且能够编辑产品14放在架子23上。使用Route Debugger进行检查,路径与路由匹配,但是当我尝试使用Route Debugger导航到路由时,它显示HTTP 404错误。有人知道为什么会发生这种情况吗?

+0

请添加您的ProductsController及其操作方法的代码。 – LorenzCK 2010-01-16 01:48:31

回答

3

那么,对于初学者来说,id1 =“”行会有问题,因为你不能做出任何可选的结果。

我刚刚在我的系统上试过,它工作得很好。

这是路线:

routes.MapRoute(
    "shelf-route", 
    "shelves/{id1}/products/{action}/{id2}", 
    new { controller = "Products", action = "List", id2 = "" } 
); 

这是控制器:

public class ProductsController : Controller 
{ 
    public string List(string id1, string id2) 
    { 
     return String.Format("ID1 = {0}, ID2 = {1}", id1, id2); 
    } 
} 

我试着像URL:

http://localhost:14314/shelves/23/products/list/14
http://localhost:14314/shelves/23/products

而且他们的工作只是鳍即

当您尝试带有“编辑”的URL时,是否记得进行编辑操作?如果没有编辑操作,您将得到一个404.

+0

感谢您的回答。事实证明,我的路由以错误的顺序列出,所以它首先与另一个匹配。我应该仔细看看Route Debugger输出。 – 2010-01-23 01:43:27