After login how to go back to the last visited page in ASP.NET

This is one of the common question I always see in .NET forums, especially by beginners.

Suppose you have many pages in your application and you are allowing the user to view few contents in the page without login and you wanted to redirect to the same page if user wish to login to view complete information.

 There are many ways to accomplish this tasks but the one I am showing here is the simplest one with very few lines of code.

You will be writing all this code in your login page.

First declare a static variable in the name strReturnURL and then assign the return URL using UrlReferrer.

static string prevPage = String.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                prevPage = Request.UrlReferrer.ToString();

            }
        }


Now in your Login button click event write below code,
protected void btnLogin_Click(object sender, EventArgs e)
{
//Here you may write the code to validate the user.
...
...
...
Response.Redirect(strReturnURL);
}


3 Comments

Comments have been disabled for this content.