这里有 2 个不同的问题。首先,在路由上设置 3 个可选参数是行不通的。只有最后一个(最右边的)参数可以是可选的。因此,您需要两条路由来启用 URL 中 0、1、2、3、4 或 5 段的所有组合。
// This will match URLs 4 or 5 segments in length such as:
//
// /Home/Index/2/3
// /Home/Index/2/3/4
//
routes.MapRoute(
name: "4-5Segments",
url: "{controller}/{action}/{id}/{id2}/{id3}",
defaults: new { id3 = UrlParameter.Optional }
);
// This will match URLs 0, 1, 2, or 3 segments in length such as:
//
// /
// /Home
// /Home/Index/
// /Home/Index/2
//
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
其次,您的 ActionLink 不匹配,因为您已将路由值指定为 id1,但在您的路由中该值是 id。所以,你需要改变ActionLink如下。
@Html.ActionLink("my text", "myActionName", "myControllerName", new { id = 3, id2 = 7, id3 = 2}, null)
这假设您已正确设置控制器:
public class myControllerNameController : Controller
{
//
// GET: /myActionName/
public ActionResult myActionName(int id = 0, int id2 = 0, int id3 = 0)
{
return View();
}
}
请参阅working demo here。
请以文本形式提供代码以供将来参考。复制和编辑图像确实很难,因此您获得答案的可能性要小得多。