2

I'm using ASP.NET MVC for creating a blog and whenever user post a comment the form is posted to the url /Post/AddComment but after successfully saving to database I want to redirect the user back to the post where they added comment for ex. http://myblog/archive/2010/11/post.aspx. How I can do that?

VJAI
  • 32,167
  • 23
  • 102
  • 164

1 Answers1

3

You can probably get the URL referrer on the AddComment Action then redirect to that.

e.g.

public ActionResult AddComment(int blogId){
    var referer = Request.UrlReferrer;
    ViewBag.Referrer = referer;
    Return View();
}

Alternatively you could pass about a ReturnUrl in the query string an access this. So if you are clicking a button or a link on the blog post page to add a comment, you could add returnurl=@Request.Url This would then allow you to access this on the 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.
}
Tim B James
  • 20,084
  • 4
  • 73
  • 103