问计于最好的方式来解决这个问题:URL路由参数名称,订单
在我的控制器我有以下作用
public ActionResult Member(string id){return View();}
在同一个控制器的另一个行动
public ActionResult Archive(string year){return View();}
在我的global.asax
routes.MapRoute(
"Archive", // Route name
"{controller}/{action}/{year}", // URL with parameters
new { controller = "Home", action = "Index", year = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id="" } // Parameter defaults
);
如果我将这个网址称为mysite.com/archive/2009,这是可行的,因为预期带有名称“year”的参数的动作和第一个路由模式适用于此请求。但是如果我打电话给这个网址mysite.com/member/john它将导致这个网址mysite.com/member?id=john 因此,看起来如果我在global.asax中的第一条路线具有相同的构造但参数名称不同,具有正确参数名称的人将拥有正确的url(mysite.com/archive/2009),但其他人不会。我该如何解决这个问题?我可以更改路由表以期望像“param”这样的通用参数名称,但我需要将该动作的所有签名更改为“param”,并且它不是非常具有描述性(参数代替年份或参数代替id)。
又一次令人困惑,我会使用会员不是年份的ID。 – dritterweg 2010-02-23 20:23:04
对不起,做了一个编辑。重点是您需要在路由中指定该操作,以便路由类可以识别要使用的路由。此外,它使用匹配的第一条路线,因此您需要首先放置更具体的路线。 – 2010-02-23 20:29:58