Adding a back button link in Asp.Net MVC

Add back button to go to previous url in AspNet MVC

 Asp.Net   
 

Posted: March 29, 2017. | By: mustafa

How to add a back button link in Asp.Net MVC dynamic web pages. To go back to previous url we can use HttpContext UrlReferrer to save the url in session object in action method.

We can refer: System.Web.HttpContext.Current.Request.UrlReferrer;

Define a ReturnBack() method in cs.

public ActionResult ReturnBack(string returnUrl)
 {
 string previousUrl = System.Web.HttpContext.Current.Request.UrlReferrer.PathAndQuery;
 if (Session["PreviousUrl"] != null)
 {
 return Redirect(Session["PreviousUrl"].ToString());
 }
 else
 {
 return Redirect(previousUrl);
 }
 }

Add html anchor linking to above method.

<a href="~/ReturnBack" accesskey="r">Return Back <span class="badge">r</span></a>

It is simple done. Suppose I have a web page with dynamic content or having edit functionality and we just return to that url and wanted to reload page variables we can save and retrieve from session object.


related posts

Back to top