我在掌握 MVC 路由时遇到了一些麻烦。我读了又读,仍然没有多大意义。我正在尝试映射路线“Teams/Summary?teamID=30”,以便它在浏览器中显示“Teams/30”。这就是我目前所拥有的,但我不明白为什么它不起作用:
//Teams routes
routes.MapRoute(
"TeamsSummary",
"Teams/{teamID}",
new { controller = "Teams", action = "Index", teamID = UrlParameter.Optional }
);
“不起作用”是指网页已加载,但仍会在浏览器地址栏中显示“Teams?teamID=30”。
有人可以帮助我了解执行如此简单的映射需要做什么吗?顺便说一句,我没有设置任何其他我认为会干扰的路由,并且它列在我的 Global.asax.cs 文件中的默认路由之前。
如果你有一些关于这个主题的好文章和教程,那也会很有帮助。谢谢。
编辑: 如果我手动输入 URL,路由工作正常。现在当我使用表单回发到控制器时会出现问题:
控制器:
public ActionResult Index(TeamsViewModel model)
{
if (model.teamID != null)
model.TeamSummary = new TeamSummaryViewModel(model.teamID);
return View(model);
}
看法:
@{
ViewBag.Title = "Teams";
}
@using (Html.BeginForm("Index","Teams",FormMethod.Get))
{
@Html.DropDownList("teamID", new SelectList(Model.Teams,"teamID","Name"))
<input type="submit" value="Filter"/>
}
@if (Model.TeamSummary != null)
{
Html.RenderPartial("_TeamSummary", Model.TeamSummary);
}
Edit2: 将原始操作更改为 TeamsController 上的索引,而不是之前的摘要。