Shuo
Shuo

Reputation: 4849

Using Response.Redirect() to a relative path

I'm working with ASP.net. My website is hosted within a subfolder test under the IIS root directory. So the url of default.aspx is http://localhost/test/Default.aspx. From default.aspx, I want to use Reponse.Redirect() with a relative path to redirect to another url within the same web site, http://localhost/test/whatever.

I tried

Response.Redirect("/whatever");

and

Response.Redirect("~/whatever");

Both of them redirect to http://localhost/whatever. Note that the Redirect method use http://localhost instead of http://localhost/test/ as the base url.

Any ideas?

Thanks.

Upvotes: 18

Views: 49835

Answers (4)

jmoreno
jmoreno

Reputation: 13571

The problem is that your application is not hosted in the directory test, it is instead hosted in the directory above it. If you want the ~ (aka tilde) to work, you need to make test the virtual path to your application, at which point your host really will be hosted in test and Response.Redirect("~/whatever") will work.

Upvotes: 1

bgs264
bgs264

Reputation: 4642

Try this (my example is VB.net)

  Dim url As String = "~/SomeDirectory/SomePage.aspx"
  Response.Redirect(url.Replace("~"c, Request.ApplicationPath))

I like to have Utils.RedirectRelative("~/SomeDirectory/SomePage.aspx") in a class somewhere but I don't know how "good practice" that is.

Upvotes: 0

Doug
Doug

Reputation: 5318

Try:

Response.Redirect("hello");

also

Response.Redirect("./hello");

Enjoy!

Upvotes: 19

0bj3ct.m3th0d
0bj3ct.m3th0d

Reputation: 555

Sorry if I'm over-simplifying or misunderstanding your question, but have you simply tried:

Response.Redirect("hello");

Upvotes: 4

Related Questions