How do i return to a specific link depending on where I came from?
I have a admin panel, and this allows to delete an answer from 2 different places. So now i want to be able to redirect to user to the link he came from
so these are the links:
/Answer/Index -> /Answer/Delete/{id}
/Statement/Edit/1 -> /Answer/Delete/{id}
I've done some research and found this
return Redirect(ControllerContext.HttpContext.Request.UrlReferrer.ToString());
so i used it in my controller
public ActionResult Delete(int id = 0)
{
answer answer = db.answer.Find(id);
if (answer == null)
{
return HttpNotFound();
}
return View(answer);
}
//
// POST: /Answer/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
answer answer = db.answer.Find(id);
db.answer.Remove(answer);
db.SaveChanges();
return Redirect(ControllerContext.HttpContext.Request.UrlReferrer.ToString());
}
the problem is that this redirect to /Answer/Delete/{id} instead of the link right before that. How can I do this??
SOLUTION thanks to Ashwini Verma
the solution of Ashwini Verma works after making this changes
public ActionResult DeleteConfirmed(int id, Uri PreviousUrl)
{
answer answer = db.answer.Find(id);
db.answer.Remove(answer);
db.SaveChanges();
return Redirect(PreviousUrl.ToString());
}