我正在使用ASP.NET MVC创建博客,每当用户发表评论时,表单都会发布到url / Post / AddComment但是在成功保存到数据库之后我想将用户重定向回他们的帖子添加评论为前。 http://myblog/archive/2010/11/post.aspx。我怎么能这样做?
答案 0 :(得分:2)
您可以在AddComment Action上获取URL引用,然后重定向到该引用。
e.g。
public ActionResult AddComment(int blogId){
var referer = Request.UrlReferrer;
ViewBag.Referrer = referer;
Return View();
}
或者,你可以在查询字符串中传递一个ReturnUrl
来访问它。因此,如果您单击博客帖子页面上的按钮或链接以添加评论,则可以添加returnurl=@Request.Url
这样您就可以在POST ActionResult上访问它。
// Get
public ActionResult AddComment(int blogId, string returnUrl){
Return View();
}
[HttpPost]
public ActionResult AddComment(BlogComment blogComment, string returnUrl){
// do your stuff then redirect to the return url.
}