我正在尝试Route
在 MVC3 中创建一个新的来实现链接http://localhost/Product/1/abcxyz
:
routes.MapRoute(
"ProductIndex", // Route name
"{controller}/{id}/{name}", // URL with parameters
new { controller = "Product", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional } // Parameter defaults
);
我Route Link
是这样使用的:
<li>@Html.RouteLink("My Link", "ProductIndex", new { controller = "Product", id = 10, name = "abcxyz" })</li>
产品索引操作:
public ViewResult Index(int id, string name)
{
var product = db.Product.Include(t => t.SubCategory).Where(s => s.SubID == id);
return View(product.ToList());
}
网址按我的预期呈现。但是当我点击它时,我收到一条 404 错误消息
HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly
更新
我把它Route
放在上面Default Route
并且 URL 工作正常。但是出现了问题。我的索引页面http://locahost
直接指向控制器的Index
动作Product
,但我希望它指向控制器的Index
动作Home