是否有可能实现post-redirect-get模式,在
asp.net-mvc中有两个重载的操作方法(一个用于GET操作,另一个用于POST操作).
在所有MVC post-redirect-get模式样本中,我已经看到了三种不同的操作方法,用于post-redirect-get过程(对应于Initial Get,Post和Redirection Get),每个都有不同的名称.在asp.net-mvc中,这真的需要至少有三种不同名称的动作方法吗?
对于Eg :(下面显示的代码是否遵循Post-Redirect-Get模式?)
public class SomeController : Controller
{
// GET: /SomeIndex/
[HttpGet]
public ActionResult Index(int id)
{
SomeIndexViewModel vm = new SomeIndexViewModel(id) { myid = id };
//Do some processing here
return View(vm);
}
// POST: /SomeIndex/
[HttpPost]
public ActionResult Index(SomeIndexViewModel vm)
{
bool validationsuccess = false;
//validate
if (validationsuccess)
return RedirectToAction("Index", new {id=1234 });
else
return View(vm);
}
}
}
谢谢你的回复.
最佳答案 从单元测试的角度思考.
如果一切都在一个动作中,那么代码将很难测试和阅读.我发现代码中没有任何问题.