The right way to do Response.Redirect from one page to another on the same site

I am sure it was asked again and again. And yes, I was looking. However, I cannot find an example that clearly demonstrates what I want to accomplish.

I'm on mywebsite/test.aspx , I want to redirect to mywebsite/testing.aspx . However, I want this redirection to work both on the server and inside the visual studio debugging. I tried

 Response.Redirect(Request.RawUrl.Replace(Request.RawUrl,"testing.aspx")) 

However, this replaces all of this.

Hope this makes sense - mywebsite/test.aspx should redirect to mywebsite/testing.aspx

+6
source share
3 answers

Use relative application URLs. ~/ represents the root path of the application, so it will work for both / and /virtual-directory/ .

 Response.Redirect("~/testing.aspx"); 
+10
source

If your page is at the same directory level, you can simply use:

 Response.Redirect("testing.aspx", false); 

If your page is at the root of the application, you can use the following command:

 Response.Redirect("~/testing.aspx", false); 

And finally, if your page is inside a subdirectory of the current page, you can use:

 Response.Redirect("MyFolder/testing.aspx", false); 
+5
source

You can use the URL relative to the root of the website. These URLs always begin with / , which means "root"

For Response.Redirect and any other URL that, as you know, will be processed by the server (for example, the url specified in server management), it is better to run the url with ~/ as root, this will help with clutter of virtual directories.

+1
source

All Articles