为什么 MVC 将路由值保留在 URL 中而不直接传递它们
Why MVC keeps route values in URL without passing them directly
我有一个问题。
假设我有这条路线:
/Guest/{var1}/{var2}
/Guest/{var1}/{var2}/edit
当我在 /Guest/123/321
页面时,我有一个 link 到 /Guest/123/321/edit?id=1
。
页面 /Guest/123/321/edit?id=1
上有一个表格,它自己张贴在同一地址。
假设我的 Actions 看起来像:
public ActionResult Index(int var1, int var2)
{
/* here is some a business logic */
return View(model);
}
[HttpGet]
public ActionResult Edit(int id)
{
/* here is some a business logic */
return View(model);
}
[HttpPost]
public ActionResult Edit(EditModel model)
{
/* here is some a business logic */
return RedirectToAction("Index");
}
问题是为什么我在提交表单后RedirectToAction("Index")
后有URL/Guest/123/321
?我的意思是 - 这太棒了。它减少了很多代码。我只是不喜欢使用我不理解的方法。 :)
我一直认为,我应该将新的 { var1 = 123, var2 = 321 }
行传递给 RedirectToAction
以保留 URL.
这是 MVC 中令人困惑的部分,之前被报告为错误1,因为许多人认为这种行为并不自然。但根据 Microsoft 的说法,此行为是设计使然。
- Unfortunately, the Codeplex issue URL was taken down and is not in the Internet archive.
行为是路由值在未明确提供时从当前请求中重用。
有些情况下效果很好,比如, but in other cases such as when ,需要手动清除ActionLink
中的值才能访问默认区域。
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, null)
我有一个问题。 假设我有这条路线:
/Guest/{var1}/{var2}
/Guest/{var1}/{var2}/edit
当我在 /Guest/123/321
页面时,我有一个 link 到 /Guest/123/321/edit?id=1
。
页面 /Guest/123/321/edit?id=1
上有一个表格,它自己张贴在同一地址。
假设我的 Actions 看起来像:
public ActionResult Index(int var1, int var2)
{
/* here is some a business logic */
return View(model);
}
[HttpGet]
public ActionResult Edit(int id)
{
/* here is some a business logic */
return View(model);
}
[HttpPost]
public ActionResult Edit(EditModel model)
{
/* here is some a business logic */
return RedirectToAction("Index");
}
问题是为什么我在提交表单后RedirectToAction("Index")
后有URL/Guest/123/321
?我的意思是 - 这太棒了。它减少了很多代码。我只是不喜欢使用我不理解的方法。 :)
我一直认为,我应该将新的 { var1 = 123, var2 = 321 }
行传递给 RedirectToAction
以保留 URL.
这是 MVC 中令人困惑的部分,之前被报告为错误1,因为许多人认为这种行为并不自然。但根据 Microsoft 的说法,此行为是设计使然。
- Unfortunately, the Codeplex issue URL was taken down and is not in the Internet archive.
行为是路由值在未明确提供时从当前请求中重用。
有些情况下效果很好,比如ActionLink
中的值才能访问默认区域。
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, null)