开发者

Using Response.Redirect() to a relative path

开发者 https://www.devze.com 2023-01-01 19:18 出处:网络
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 w开发者_Sta

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 w开发者_StackOverflow中文版ant 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.


Try:

Response.Redirect("hello");

also

Response.Redirect("./hello");

Enjoy!


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

Response.Redirect("hello");


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消