Note: This article was originally published in 2009. Some steps, commands, or software versions may have changed. Check the current .Net documentation for the latest information.

Prerequisites

Before you begin, make sure you have:

  • Visual Studio or .NET CLI installed
  • .NET Framework or .NET Core SDK
  • Basic C# programming knowledge

I am trying to use the login.aspx page in such a way that when a user tries to access any given page, it will redirect them to the login screen if they are not authenticated and after a successful authentication go back to the page they were trying to access. My issue is that when I go to the page and get redirected, I don’t go back to the page I came from. Here is a sample of what my login.aspx page has for an URL: (http://localhost:49716/Login.aspx?ReturnUrl=/MaintainGroups.aspx) As you can tell, ASP.Net is smart enough to include the return to URL, however, how can I make use of that to redirect my users back to that page? Also, in general, how can you read a parameter in the URL query string? Thanks!


The following code would address the issue. Note that you can read any Paramater by doing a QueryString from the Request. ReturnUrl is the one that pertains to this particular example: if (Request.QueryString != null) { string ReturnUrl = Request.QueryString.Trim(); //Navigate to the ReturnUrl } else //Navigate to a specific home page

Summary

You’ve successfully learned how do you read a parameter from the url?. If you run into any issues, double-check the prerequisites and ensure your .Net environment is properly configured.