在我的控制器的操作中,我必须执行同一控制器的另一个操作。这两个操作都处于相同的安全环境中。我是否必须调用RedirectAction来执行其他操作,还是必须创建一个可以调用这两个操作的共享方法?Asp.Net MVC RedirectAction或共享方法
如实施例用于使用RedirectAction:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Search(string value)
{
IPresenter presenter = new Presenter();
List<Item> items = presenter.GetList(value);
if (items.Count > 1)
return base.View("List", items);
else
return base.RedirectAction("Detail", new { id = items.First().Id });
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Detail(int id)
{
IPresenter presenter = new Presenter();
return base.View(presenter.GetItemById(id));
}
和示例使用共享方法:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Search(string value)
{
IPresenter presenter = new Presenter();
List<Item> items = presenter.GetList(value);
if (items.Count > 1)
return base.View("List", items);
else
return this.GetDetail(id);
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Detail(int id)
{
return this.GetDetail(id);
}
private ActionResult GetDetail(int id)
{
IPresenter presenter = new Presenter();
return base.View(presenter.GetItemById(id));
}
在共享方法的情况下,我有一个http请求小于在RedirectAction的情况下,但与RedirectAction,我有一个更自然的Asp.Net MVC方式的流量。
您认为哪种情况最好,为什么?如果两种情况都很好,那么情况是好还是不好?
注: 我故意不使用后的查询,因为我知道,在这种情况下,PRG Pattern是必不可少的,以防止不必要的行为,其中多个岗位可以同时使用的后退按钮被发送到服务器的客户端浏览器。
非常感谢。