【发布时间】:2018-08-08 17:48:12
【问题描述】:
我正在尝试将MapRoute 与多个参数一起使用,但它无法正常工作。
我的RegisterRoutes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"TestRoute", // Route name
"{controller}/{action}/{id}/{tID}", // URL with parameters
new { controller = "Test", action = "Index", id = "", tID = "" } // Parameter defaults
);
}
我的测试控制器:
public class TestController : Controller
{
public ActionResult Index(int? id, int? tID)
{
// Some code to create viewModel
return View(viewModel);
}
}
我从Html.ActionLink 调用此方法:
@Html.ActionLink(@item.Description, "Index", "Test",
new { id = @item.CatID, tID = @item.myTID }, null)
但是,它返回的 url 为:/Test/Index/3?tID=264981 而不是 /Test/Index/3/264981
有人知道我做错了什么吗?
【问题讨论】:
-
更具体的路线需要先。
-
按照佩德罗所说的去做。首先是
TestRoute路由,然后是Default路由。 -
有效!谢谢
标签: c# asp.net asp.net-mvc routes asp.net-mvc-routing