【发布时间】:2017-06-26 08:51:20
【问题描述】:
我正在使用 ASP.net MVC5,我有以下内容:
[Route("Edit/{id}")]
[HttpGet]
public ActionResult Edit(int id)
{
// do stuff
return View(viewModel);
}
[Route("Edit")]
[HttpPost]
public ActionResult Edit(DraftViewModel draft)
{
if (!ModelState.IsValid) return View(draft);
// do stuff
return RedirectToAction("Index");
}
我希望这会产生像这样的漂亮 URL:
草稿/编辑/5
相反,我得到了这个:
草稿/编辑?id=5
这是为什么?如何使用基于属性的路由获得漂亮的 URL?生成带有丑陋 URL 的链接的代码是这样的:
@Html.ActionLink("Edit", "Edit", "Draft", new { id = draft.Id }, new { @class = "btn btn-primary btn-xs" })
更新
当我删除[POST] 操作并且只有[GET](当然没用)时,GET 上的 URL 看起来很漂亮!因此,当我有两条同名(但动词不同)的路线时,框架就会出错!
【问题讨论】:
-
什么产生了
Draft/Edit?id=5?你能分享那个代码吗? -
@Shyju 我已经添加了那个代码
标签: c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing