1

现在我有两个动作,一个用于HttpGet,一个用于HttpPost,而HttpGet一个主要用于子动作,所以我的代码如下所示:

[ActionName("Comments")]
[HttpGet]
public void CommentList(string postId) {
    // This is for SEO
    if (!ControllerContext.IsChildAction) {
        return Redirect("/post/" + postId);
    }

    // Get list
    return View("CommentList", list);
}

[ActionName("Comments")]
[HttpPost]
public void PostComment(Comment comment) {
    // Save to DB
    // Get the parent post
    // return View("Post", post);
}

不幸的是,在我的Post视图中,我将CommentList呈现为子操作:

@Html.Action("Comments", new { postId = Model.Id });

问题是,当我使用POST请求提交评论时,在将评论保存到数据库后,它会执行Post视图,从而呈现“评论”子操作。在这里,当“评论”子动作被渲染时,HttpVerb 仍然是POST,所以路由器给出了PostComment动作,它再次(在一些验证失败之后)渲染Post视图,它渲染“评论”子动作......

它会导致无限递归,导致StackOverflowException我的网络服务器崩溃。

那么,有什么方法可以强制我@Html.Action("Comments")执行CommentList操作,我可以将HttpVerb或类似的东西传递给路由器吗?

谢谢

4

1 回答 1

0

你为什么不使用我们 Post Redirect Get 喜欢的 PRG

[ActionName("Comments")]
[HttpPost]
public void PostComment(Comment comment) {
    // Save to DB
    // redirect to the Get Method
    return RedirectToAction("Comments",new {postId=comment.postId.ToString()});
}
于 2012-12-26T07:49:14.583 回答