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?
Asked
Active
Viewed 3,449 times
1 Answers
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
-
2I got a link here will help others http://stackoverflow.com/questions/1471188/how-do-i-get-the-referrer-url-in-an-asp-net-mvc-action – VJAI May 19 '11 at 13:47
-
That doesn't cover enough details. – SepehrM Mar 31 '15 at 20:25
-
@SepehrM added more details for you ;) This was an old answer when i didn't give a lot of detail – Tim B James Apr 01 '15 at 08:11